如何让快速/的NodeJS CORS
2015-09-19
展开全部
CORS规格书号只需要OPTIONS调用先于POST或GET如果POST或GET有任何非简单内容或标题在里面。 参见“预检请求”
2. 尝试将控制传递给下一个匹配的路由。如果快递是优先个匹配app.get路线,那么它不会继续到选择路线,除非你做(未来的)这样的:
app.get('somethingelse', function(req, res, next) {
//..set headers etc.
next();
});
在条款CORS的东西,我把它放在一个中间件是运作良好
//CORS middleware
var allowCrossDomain = function(req, res, next) {
res.header('Access-Control-Allow-Origin', config.allowedDomains);
res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE');
res.header('Access-Control-Allow-Headers', 'Content-Type');
next();
}
//...
app.configure(function() {
app.use(express.bodyParser());
app.use(express.cookieParser());
app.use(express.swww.hbbz08.com ession({ secret: 'cool beans' }));
app.use(express.methodOverride());
app.use(allowCrossDomain);
app.use(app.router);
app.use(express.static(__dirname + '/public'));
});
3. 留在路由的想法。这段代码:
app.all('/*', function(req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "X-Requested-With");
next();
});
4. 我已经适合明示或连接中间件。它支持OPTIONS要求预检检查。请注意,它将使CORS访问任何东西,你可能要在支票放 CodeGo.net,如果你想限制接取。
app.use(function(req, res, next) {
var oneof = false;
if(req.headers.origin) {
res.header('Access-Control-Allow-Origin', req.headers.origin);
oneof = true;
}
if(req.headers['access-control-request-method']) {
res.header('Access-Control-Allow-Methods', req.headers['access-control-request-method']);
oneof = true;
}
if(req.headers['access-control-request-headers']) {
2. 尝试将控制传递给下一个匹配的路由。如果快递是优先个匹配app.get路线,那么它不会继续到选择路线,除非你做(未来的)这样的:
app.get('somethingelse', function(req, res, next) {
//..set headers etc.
next();
});
在条款CORS的东西,我把它放在一个中间件是运作良好
//CORS middleware
var allowCrossDomain = function(req, res, next) {
res.header('Access-Control-Allow-Origin', config.allowedDomains);
res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE');
res.header('Access-Control-Allow-Headers', 'Content-Type');
next();
}
//...
app.configure(function() {
app.use(express.bodyParser());
app.use(express.cookieParser());
app.use(express.swww.hbbz08.com ession({ secret: 'cool beans' }));
app.use(express.methodOverride());
app.use(allowCrossDomain);
app.use(app.router);
app.use(express.static(__dirname + '/public'));
});
3. 留在路由的想法。这段代码:
app.all('/*', function(req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "X-Requested-With");
next();
});
4. 我已经适合明示或连接中间件。它支持OPTIONS要求预检检查。请注意,它将使CORS访问任何东西,你可能要在支票放 CodeGo.net,如果你想限制接取。
app.use(function(req, res, next) {
var oneof = false;
if(req.headers.origin) {
res.header('Access-Control-Allow-Origin', req.headers.origin);
oneof = true;
}
if(req.headers['access-control-request-method']) {
res.header('Access-Control-Allow-Methods', req.headers['access-control-request-method']);
oneof = true;
}
if(req.headers['access-control-request-headers']) {
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询