【发布时间】:2012-12-24 14:00:35
【问题描述】:
我有一个带有跨域设置的 express.js 应用程序
var allowedHost = {
'http://localhost:3001': true,
'http://localhost:7357': true
};
var allowCrossDomain = function(req, res, next) {
if(allowedHost[req.headers.origin]) {
res.header('Access-Control-Allow-Credentials', true);
res.header('Access-Control-Allow-Origin', req.headers.origin)
res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE,OPTIONS');
res.header('Access-Control-Allow-Headers', 'X-CSRF-Token, X-Requested-With, Accept, Accept-Version, Content-Length, Content-MD5, Content-Type, Date, X-Api-Version');
next();
} else {
res.send(403, {auth: false});
}
}
我的客户端 (backbone.js) 也被配置为接受跨域,这部分一切正常..
现在,在我的 express.js 应用程序中(在端口 3001 上运行),我正在尝试访问这样的简单页面:
app.get('/app', function(req, res, next){
return res.render("" + __dirname + "/views/app", {
title: 'hello world'
});
});
如果我调用 url localhost:3001/app 例如我有 403 错误,因为 req.headers.origin 未定义,你知道吗?
我应该如何告诉我正常的 express.js 路由来应对跨域检查?
非常感谢您对此问题的任何帮助;-)
【问题讨论】: