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

后端 未结 4 982
长发绾君心
长发绾君心 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:09

    Try something like this:

    connect: {
      options: {
        port: 9000,
        // Change this to '0.0.0.0' to access the server from outside.
        hostname: 'localhost',
        livereload: 35729,
    
        // remove next from params
        middleware: function(connect, options) {
          return [
            function(req, res, next) {
              res.setHeader('Access-Control-Allow-Origin', '*');
              res.setHeader('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE');
              res.setHeader('Access-Control-Allow-Headers', 'Content-Type');
    
              // don't just call next() return it
              return next();
            },
    
            // add other middlewares here 
            connect.static(require('path').resolve('.'))
    
          ];
        }
        },
        },
    
    0 讨论(0)
  • 2021-01-11 15:10

    Grunt connect comes with multiple middlewares, stored as functions in an array. When you set middleware by returning an array, you override the existing middleware responsible for serving your pages.

    Going off ansorensen's comment of the documentation, https://github.com/gruntjs/grunt-contrib-connect#middleware the relevant section being.

    options: {
        middleware: function(connect, options, middlewares) {
          // inject a custom middleware into the array of default middlewares
          middlewares.unshift(function(req, res, next) {
            if (req.url !== '/hello/world') return next();
    
            res.end('Hello, world from port #' + options.port + '!');
          });
    
          return middlewares;
        },
    },
    

    Middleware earlier in the array take effect before those later in the array.

    So what you would want is

    connect: {
        options: {
            port: 9000,
            // Change this to '0.0.0.0' to access the server from outside.
            hostname: 'localhost',
            livereload: 35729,
    
            // remove next from params
            middleware: function(connect, options, middlewares) {
                middlewares.unshift(function(req, res, next) {
                    res.setHeader('Access-Control-Allow-Origin', '*');
                    res.setHeader('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE');
                    res.setHeader('Access-Control-Allow-Headers', 'Content-Type');
    
                    return next();
                });
    
                return middlewares;
            }
        },
    },
    
    0 讨论(0)
  • 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;
    }
    
    0 讨论(0)
  • 2021-01-11 15:28
    connect: {
      options: {
        port: 9000,
        // Change this to '0.0.0.0' to access the server from outside.
        hostname: 'localhost',
        livereload: 35729,
        middleware: function(connect, options, next) {
          return [
            function(req, res, next) {
             res.header('Access-Control-Allow-Credentials', true);
             res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
             res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE,OPTIONS');
             next();
          }];
         }
       };
    

    this will helps you in getting the get call Access-Control-Allow-Credentials

    0 讨论(0)
提交回复
热议问题