bezzoon
bezzoon

Reputation: 2019

Failure to set and get cookies in Express.js

app.use(express.cookieSession());
...


res.cookie('username', userName, {  httpOnly: false});
console.log(res.cookie);

Logs this text:

[Function]

Which is not something that I have seen before. I am a little bit confused about how to set and get cookies in express.

Upvotes: 0

Views: 211

Answers (2)

ragamufin
ragamufin

Reputation: 4162

res.cookie is a function. You are using it to set the cookie in

res.cookie('username', userName, {  httpOnly: false});

So your console.log is right.

To see the cookie after you set it you'll need to refresh your browser and then outputting req.cookies or req.signedCookies will show you the contents of the cookie that was set. Take a look at the last line of the cookie section in express guide

Upvotes: 2

Hacknightly
Hacknightly

Reputation: 5164

Cookies in Express can be read from from req.cookies.

i.e

app.get('/foo', function(req, res){
 console.log(req.cookies);
 res.send(200);
});

Also, don't forget to include the cookie parser middleware.

Upvotes: 0

Related Questions