Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 265
danchohen Avatar asked Jul 24 '14 11:07

danchohen


1 Answers

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....

like image 85
Vitalik Teremasov Avatar answered Oct 05 '22 23:10

Vitalik Teremasov