NodeJS Cookie


How to get Cookie

function doRequest(req, res) {
    var ck = req.headers.cookie;

How to set cookie

Use setHeader

res.setHeader('Set-Cookie',['lasturl=' + path.pathname]);

[] is array of javascript, value which is set is ‘key=value’.

Cookie Module

To mange cookie, we have options.
Install cookie module using npm

npm install coolie     # install cookie module into node_modules/cookie

How to use?

Module include

var cookie = require('cookie');

Serialize cookie

var ck1 = cookie.serialize('lasturl', path.pathname, { maxAge : 100 });  
// key, value, options
res.setHeader('Set-Cookie', ck1);		// Set cookie

Parse cookie

var ck = cookie.parse(req.headres.cookie);   // parse using cookie module