一个用于SPA的节点快速API,具有以下内容:
const express = require ('express');
const bodyParser = require ('body-parser');
const cookieSession = require ('cookie-session');
const cors = require ('cors');
const app = express();
app.use (cors());
app.use (cookieSession({
name: 'app',
keys: ['a-key'],
maxAge: 7 * 24 * 3600 * 1000, // 7 days
}));
const router = express.Router();
app.use ('/api', router);
router.use ('/login', login);
router.use ('/logout', logout);
router.use ('/users', users);
etc ...
app.listen (port);
来自 SPA 的请求使用以下fetch
:
try {
await fetch ('/api/users');
... etc
}
Express在localhost:3000
mo'上运行,开发SPA在localhost:5000
上运行。
浏览器中未设置 Cookie;console.log
的req.session
显示空的{}
对象。
我试过:
credentials: true,
origin: 'http://localhost:5000'
在cors
启动
我也尝试过:
secure: false,
httpOnly: false,
sameSite: false
在cookieSession
启动
中我也尝试过包括:
credentials: 'include'
关于fetch
请求
这些都不起作用,无论是单独还是一起。
什么给?
看起来
fetch (url, {
credentials: 'include'
});
实际上需要SPA,一旦我注销并再次登录,会话在浏览器中设置正确。