express cookie parser | Create cookies using express | Angular | ASP.NET Tutorials

For Consultation : +91 9887575540

Stay Connected :

express cookie parser

Environment setup:

npm init
npm install express --save
npm install cookie-parser --save


What are Cookies in express

  • This is client-side data storage technique.
  • Small data files
  • Data sent back to the client from the server which stored in cookies.
  • Cookie always sent to the server with each request.
  • Also helps in session management.

How to implement Cookies in express

  • Install middleware for cookie parser
  • Npm install cookie-parse –save
  • Create Cookies :

       res.cookie(‘cookiename‘, ‘cookievalue’)

Set Cookies with the expiration time

  • res.cookie(cookiename, ‘value’, {expire: 360000 + Date.now()});
  • Set Cookies expiration time using maxAge property.
  • res.cookie(cookiename, ‘value’, {maxAge: 360000});

Deleting Cookies


var express = require('Express');
// Pass reference of cookie parser
var cookieparser = require('cookie-parser');

var app = express();
app.get("/", function(req, res){
    // Create cookie without expiration time 
//res.cookie('mycookie', 'yogesh sharma').send('Cookies has been set now');
// Set cookies with expiration time 
// res.cookie('mycookie2', "sharma", {expire : Date.now()+ 36000 }).send('cookie has been set');
// Set Cookies expiration using maxAge
res.cookie('mycookie3', "sharma", {maxAge :  36000 }).send('cookie has been set');

app.get("/showcookie", function(req, res){
    app.get("/clearcookie", function(req, res){
     res.clearCookie('mycookie2').send('cookie has been deleted');