Grunt connect task and middleware Access-Control-Allow-Origin

后端 未结 4 980
长发绾君心
长发绾君心 2021-01-11 14:51

I would like to allow access to cross origin calls which I need to be able to perform rest API calls to the server.

My connect grunt task is configured as follows:

4条回答
  •  北荒
    北荒 (楼主)
    2021-01-11 15:16

    Nod to bpaul, who set me on the path to correct answer. The format of a response to a similar question will work here.

    Replace 'next' with middlewares, and push your anonymous function into the middleware array before returning it:

    middleware: function(connect, options, middlewares) {
    
        middlewares.unshift(function(req, res, next) {
            res.setHeader('Access-Control-Allow-Credentials', true);
            res.setHeader("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
            res.setHeader('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE,OPTIONS');
            next();
        });
    
        return middlewares;
    }
    

提交回复
热议问题