如何让快速/的NodeJS CORS
发布网友
发布时间:2022-04-19 10:01
我来回答
共1个回答
热心网友
时间:2022-04-22 11:56
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']) {