September 27, 2022

Blog @ Munaf Sheikh

Latest news from tech-feeds around the world.

5 Express Middleware Libraries Every Developer Should Know | by Bhagya Vithana | Dec, 2021

Cookie-parser is a middleware that transfers cookies with client requests.

Cookie-parser uses the req.cookies property to access Cookie data. After parsing, the req.cookies object holds cookies sent by request in JSON format.

It is capable of parsing both unsigned and signed cookies.

Cookie-parser has over 407K NPM downloads per week and 2K GitHub ratings.

Features of Cookie-parser

  • The decode function is there to decode the value of the cookie.
  • Handle cookie separation and encoding.
  • Can enable signed cookie support by passing a secret string.
  • supports special “JSON cookies” with JSON.parse.


Use the following command to install cookie-parser.

npm install --save cookie-parser

Usage of Cookie-parser

After installation, you have to include it in our index.js file to use it.

var cookieParser = require('cookie-parser');

Let’s create a new route in your Express app to set a new cookie.

var express = require('express');
var app = express();
app.get('/', function(req, res){
res.cookie('name', 'express').send('cookie set');

You can use the following command to verify the cookie values on the client side.


To view cookies from your server, add the following code to a route in the server console.

console.log('Cookies: ', req.cookies);

As a result, the next time you request this route, you’ll get the following response.

Cookies: { name: 'express' }

Source link