简体   繁体   中英

Nodejs express cookies are not added

I'm trying to build a nodejs app and I have to define cookies. This is some of the code:

var app = express();

app.set('port', process.env.PORT || 3000);
app.set('views', __dirname + '/views');
app.set('view engine', 'ejs');
app.use(express.favicon());
app.use(express.cookieParser());
app.use(express.json());
app.use(express.urlencoded());
app.use(express.methodOverride());
app.use(app.router);
app.use(express.static(path.join(__dirname, 'public')));
...
app.get('/', routes.indexGET);
app.post('/login', routes.loginPOST);
app.post('/number', routes.numberPOST);
...
res.cookie('number', req.body.number, {secure: true, httpOnly: true, maxAge: 1209600000});
console.log(req.cookies); //Returns {}

Why isn't the cookie added? I know that this question has been asked here but I didn't really fined any fine answer. Even "correct" answers (put the cookieParser before the router) wasn't helpful. Any suggestions? Thanks.

Not sure why, but it doesn't work for me too if secure: true option used in res.cookie('number', req.body.number, {secure: true, httpOnly: true, maxAge: 1209600000})

Don't use it and it's going to work... I guess using it needs a TLS connection established....

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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