【发布时间】:2013-08-15 17:01:57
【问题描述】:
我正在尝试使用 express 和 angularjs 创建一个简单的登录。 angular js 应用程序在单独的服务器(grunt 服务器 localhost:9000)上运行,而 express 应用程序在另一个端口上运行。对于我的快速应用程序,我设置了以下标头:
app.all('/*', function(req, res, next) {
res.header("Access-Control-Allow-Origin", "http://localhost:9000");
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
res.header("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE");
res.header("Access-Control-Allow-Credentials", "true");
next();
});
我正在运行 angular 1.0.7,这意味着我可以在配置步骤中设置一些默认值:
// Add COR ability
$httpProvider.defaults.useXDomain = true;
delete $httpProvider.defaults.headers.common['X-Requested-With'];
以及 $http 请求上的 withCredentials:
$http({
method : "GET",
withCredentials : true,
url : apiEndpoint + '/session'
}, success);
所以在使用我的应用程序登录后,会创建一个会话,并且可以在响应中看到一个 cookie。使用 chrome 调试工具,我可以看到连接会话 cookie 正在与下一个后续请求(上面的会话调用)一起发送。但是,在服务器上,req.session 属性仍然为空。当然,我可以使用 express 托管 angular 应用程序并解决所有这些问题,但我宁愿将两个项目/服务器分开。
这是 /session 请求的视图,连接会话 cookie 附加到请求:
【问题讨论】:
标签: javascript angularjs express connect cors