【发布时间】:2014-02-05 15:23:24
【问题描述】:
我想允许访问我需要能够对服务器执行 REST 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();
}
];
}
},
},
当我运行 grunt 服务器时,我得到了Cannot GET /。
在没有中间件配置的情况下,应用程序可以正常工作并且索引文件已正确加载。
你能指导我做错了什么或错过了什么吗?
关于我的 gruntfile 的更多细节是我正在使用 yeoman angular seed 应用程序作为我的应用程序的基础。
【问题讨论】:
-
我不认为中间件函数需要“next”参数。
-
是的,上面那行应该是:middleware: function(connect, options, middlewares)。 github.com/gruntjs/grunt-contrib-connect#middleware,中间件下的第二个例子。
标签: javascript node.js angularjs gruntjs grunt-contrib-connect