简体   繁体   English

无法在Express中使用Cookie-parser设置Cookie

[英]Unable to Set Cookie with `cookie-parser` in Express

I have installed cookie-parser with Express to use within my React application. 我已经在Express中安装了cookie-parser ,以在我的React应用程序中使用。 I'm trying to set a cookie so that I can begin looking into sessions for a login system. 我正在尝试设置一个cookie,以便可以开始研究登录系统的会话。

When setting a cookie with the code below, the console log will only ever output Cookies: {} . 使用以下代码设置Cookie时,控制台日志将仅输出Cookies: {} I'm unable to get a cookie to set. 我无法设置Cookie。

cookies.js cookies.js

var express = require('express');
var router = express.Router();

router.get('/', function(req, res) {
    res.cookie('test-cookie', 'test-value', {
        maxAge: 60000
    });
    console.log('Cookies: ', req.cookies);
})

module.exports = router;

I'm using fetch('/cookies') to call cookies.js from my app.js main file. 我正在使用fetch('/cookies')从我的app.js主文件中调用cookies.js

When refreshing my application, the console log outputs Cookies: {} . 刷新我的应用程序时,控制台日志将输出Cookies: {} I cannot see a cookie set within the developer tools Application tab either. 我也无法在开发人员工具的“ Application标签中看到设置的cookie。

Is there any reason I'm unable to set cookies? 有什么原因我无法设置Cookie?

If I add the following to fetch('/cookies') , the cookie is then set: 如果我将以下内容添加到fetch('/cookies') ,则设置cookie:

fetch('/cookies', {
    credentials: 'include'
})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM