Javascript Grunt连接任务和中间件访问控制允许源
我想允许访问跨源调用,我需要能够对服务器执行RESTAPI调用 我的connect grunt任务配置如下:Javascript Grunt连接任务和中间件访问控制允许源,javascript,node.js,angularjs,gruntjs,grunt-contrib-connect,Javascript,Node.js,Angularjs,Gruntjs,Grunt Contrib Connect,我想允许访问跨源调用,我需要能够对服务器执行RESTAPI调用 我的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,
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服务器时,我得到了无法得到/
。
如果没有中间件配置,应用程序将正常工作,并且索引文件已正确加载
你能告诉我我做错了什么或错过了什么吗
有关我的GrunFile的更多详细信息是,我正在使用yeoman angular seed应用程序作为应用程序的基础。尝试以下操作:
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('.'))
];
}
},
},
这将帮助您获得get call Access Control Allow凭证,并向bpaul点头,bpaul为我设置了正确答案的路径。对a的响应格式将在此处起作用 用中间件替换“next”,并在返回匿名函数之前将其推入中间件阵列:
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;
}
Grunt connect附带多个中间件,作为函数存储在一个数组中。通过返回数组设置中间件时,将覆盖负责为页面提供服务的现有中间件 离开ansorensen对文件的评论,相关章节如下
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;
},
},
阵列中较早的中间件先于阵列中较晚的中间件生效
所以你想要的是
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;
}
},
},
我认为中间件函数不需要“next”参数。是的,上面的行应该是:middleware:function(connect、options、middleware),Middleware下的第二个示例。我刚刚尝试了您的更改,但仍然无法使应用程序正常运行。有关我的GrunFile的更多详细信息是,我正在使用yeoman angular seed应用程序作为应用程序的基础。如果您使用yeoman,您应该在中间件中添加类似的内容:
connect.static(require('path').resolve(yeomanConfig.app))代码>这仍然在firebug中为我提供了“阻止跨源请求”。要使CORS正常工作,除了上面的设置外,还需要做什么?尝试了更改,但仍然没有成功。。不知道我做错了什么上述操作将抛出错误,因为res没有header方法。您需要使用res.setHeader.Yes,我更喜欢这种方法。谢谢
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;
}
},
},