So I have a route in my Express app with two middleware in them:
app.foo('/thisRoute', fancyMiddleware.one, fancyMiddleware.two);
Both middlwares function in order just fine. However, in fancyMiddleware.one I have this:
var one = function(req, res, next) {
...
...
res.cookie('myCookie', data, {maxAge: 3600000});
console.log(req.cookies.myCookie)
return next();
}
To test everything I'm using PostMan to test all my requests.
The logged output for req.cookies.myCookie always returns undefined. But in the Body tab I can see that the cookie is present.
If I log out the same cookie in fancyMiddleware.two its also undefined.
why is this returning undefined?
EDIT: So, with a few answers of "why" being given, I now realize I should have also asked:
How do I read the cookie I just set?
I dont really need it right after I set it in fancyMiddleware.one, but I do need it in fancyMiddleware.two
EDIT 2: I forgot to mention I'm working with an Express 3 setup. Probably relevant.
You are setting the cookie on the response-object res, but you are asking it from the request req.
The req.cookies is populated only once, when the cookie parser middleware is executed.
res.cookie() immediately sets the Set-Cookie header, so you'll have to use res.get('Set-Cookie') to see the current values.