express cookie return undefined express cookie return undefined express express

express cookie return undefined


Cookies are set in HTTP Headers. res.cookie() just sets the header for your HTTP result, but doesn't actually send any HTTP. If your code was syntactically correct and it ran, it would actually just sit and not return anything. I also fixed some syntax bugs in your code in this app.get():

app.get('/', function(req, res) {    res.cookie('cart', 'test', {maxAge: 900000, httpOnly: true});    res.send('Check your cookies. One should be in there now');});


You need to send something out, or at least call res.end(), after setting the cookie. Otherwise all res.cookie() does is add some headers to a list of headers that will be sent out later.


Set cookie name to value, where which may be a string or object converted to JSON. The path option defaults to "/".

res.cookie('name', 'tobi', { domain: '.example.com', path: '/admin', secure: true });

Here is the Link for more detail

http://expressjs.com/api.html#res.cookie