Last active
April 26, 2017 16:57
-
-
Save aponxi/865b2c3ecfbc2998ee58ba2f098f7362 to your computer and use it in GitHub Desktop.
Node JS Express Cookie Snippet
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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