【发布时间】:2017-10-26 03:30:36
【问题描述】:
大家好,我有一个 Angular 4 应用程序在我的本地机器上调用 Express API,这些应用程序在不同的端口(4200 和 3000)上运行。在我的 Express 应用中,我已配置:
app.use((req, res, next) => {
console.log(req.headers.origin);
res.header('Access-Control-Allow-Origin', req.headers.origin);
res.header('Access-Control-Allow-Credentials', true);
res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE');
res.header('Access-Control-Allow-Headers', 'X-Requested-With, X-HTTP-Method-Override, Content-Type, Accept');
if ('OPTIONS' == req.method) {
res.sendStatus(200);
} else {
next();
}
});
和我的 cookie 配置:
app.use(session({
resave: true,
saveUninitialized: true,
secret: process.env.SESSION_SECRET,
cookie: {
httpOnly: true,
expires: cookieExpirationDate // use expires instead of maxAge
}
}));
在 Angular 4 中,请求被调用
this.http.post(environment.services + '/login', user, {withCredentials: true});
一切都在 localhost 中运行。但随后这个应用程序被部署到不同的 Heroku 应用程序。并且在第一次通话中设置了会话,但在其他通话中不再存在。我错过了什么吗?
【问题讨论】: