Grunt连接任务和中间件Access-Control-Allow-Origin

sha*_*nti 11 javascript node.js angularjs gruntjs grunt-contrib-connect

我想允许访问跨源调用,我需要能够对服务器执行其他API调用.

我的connect grunt任务配置如下:

    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.setHeader('Access-Control-Allow-Origin', '*');
          res.setHeader('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE');
          res.setHeader('Access-Control-Allow-Headers', 'Content-Type');
          next();
        }
      ];
    }
  },
},
Run Code Online (Sandbox Code Playgroud)

当我运行grunt服务器时,我得到了Cannot GET /.没有中间件配置,应用程序正在运行,索引文件已正确加载.

你能指导我做错我做错了吗?

关于我的gruntfile的更多细节是我使用yeoman角度种子应用程序作为我的应用程序的基础.

bpa*_*aul 5

尝试这样的事情:

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('.'))

      ];
    }
    },
    },
Run Code Online (Sandbox Code Playgroud)