我有一个节点后端,它应该允许来自我在 localhost:3000 上运行的前端应用程序的跨源请求。因此,我已将 cors 策略限制在我的域中。
import csrf from 'csurf';
app.use(
cors({
origin: 'http://localhost:3000',
credentials: true
})
);
const csrfProtection = csrf({
cookie: {
maxAge: 900,
domain: 'http://localhost:3000'
}
})
router.get('/csrfToken', csrfProtection, async (req, res, next) => {
res.json({ token: req.csrfToken() });
});
Run Code Online (Sandbox Code Playgroud)
当我现在向我的服务器端点(在 localhost:5000 上运行)发出请求时,它向我返回以下错误,即无法设置 cookie。
fetch('http://localhost:5000/csrfToken', {
method: 'GET',
credentials: 'include'
})
Run Code Online (Sandbox Code Playgroud)