Skip to content

Instantly share code, notes, and snippets.

@aponxi
Last active April 26, 2017 16:57
Show Gist options
  • Save aponxi/865b2c3ecfbc2998ee58ba2f098f7362 to your computer and use it in GitHub Desktop.
Save aponxi/865b2c3ecfbc2998ee58ba2f098f7362 to your computer and use it in GitHub Desktop.
Node JS Express Cookie Snippet
//use npm install cookie-parser --save
//put the following snippet inside a route like: router.post('/save', function (req, res, next) {
//set cookie
var cookie = req.cookies['profile_id'];
if (cookie === undefined) {
try {
res.cookie('profile_id', profile_id, {
path : '/survey',
expires : new Date(Date.now() + 900000),
httpOnly : false
});
console.log('cookie created successfully');
}
catch (Error) {
console.error(Error);
}
}
else {
// yes, cookie was already present
console.log('cookie exists', cookie);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment