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....
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With