咕噜连接任务和中间件访问控制允许来源 [英] Grunt connect task and middleware Access-Control-Allow-Origin
本文介绍了咕噜连接任务和中间件访问控制允许来源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想允许访问,我需要能够执行REST API调用服务器交叉起源的电话。
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:
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();
}
];
}
},
},
当我跑我得到繁重的服务器无法GET /
。
如果没有配置中间件应用程序是否工作正常,索引文件正确。
When I run the grunt server I am getting Cannot GET /
.
Without the middleware configuration the app is working and the index file is loaded correctly.
您能指导我什么,我做错了或丢失了呢?
Could you guide me to what I am doing wrong or missing out?
我的gruntfile更多的细节是,我使用的自耕农角种子的应用,因为我的基础到应用程序。
Some more details about my gruntfile is that I am using the yeoman angular seed app as my base to the app.
推荐答案
尝试是这样的:
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('.'))
];
}
},
},
这篇关于咕噜连接任务和中间件访问控制允许来源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文