Open In App

Express Cookie-Parser – Signed and Unsigned Cookies

A cookie is a piece of data that is sent to the client-side with a request and is stored on the client-side itself by the Web Browser the user is currently using. With the help of cookies –

The session makes requests to all the servers using a secret Id. The information is stored on the server that is linked to this secret ID.
To make use of cookies in our application, cookie-parser middleware is used. To install it, write the following command –



npm install cookie-parser@latest --save

Also, to install express middleware write the following command –

npm install express@latest --save

These commands will install the latest versions of cookie-parser and express.
Cookie-parser middleware is used to parse the cookies that are attached to the request made by the client to the server. Therefore, to use cookie-parser, we will write the following lines of code in our JavaScript file –




const cookieParser = require('cookie-parser');
const express = require('express')
  
const app = express();
app.use(cookieParser());

Let’s look at an example of how to setup a new cookie. Create a new file named “index.js”. For setting up and assigning a name to a cookie, follow the code –




const express = require('express');
const cookieParser = require('cookie-parser');
  
const app = express();
  
app.get('/', (req, res) => {
   res.cookie('name', 'GeeksForGeeks').send('Cookie-Parser');
});
  
app.listen(3000, (err) => {
    if(err){ console.log(err) } 
    else { console.log('Success!!') }
});

Here, we sent the cookie to the new route and set the name of the cookie as ‘GeeksForGeeks’. In the last block of code, our server is listening to the port 3000 with a callback function. If there will be an error then the callback function will return the error else it will return “Success”.
Now, run the following code with the command –



node index.js

To check if the cookie is set or not, just go to this link after successfully setting up the server. Open the console and write the command as –

document.cookie

You will get the output as –

"name=GeeksForGeeks"

Also, the cookie-parser middleware populates the req.cookies with name that is sent to the server. Here, in our case, we can add the following line of code in our route –

console.log(req.cookies)

The output of the above line will be –

{ name: 'GeeksForGeeks' }

Methods for cookie-parser

Depending on the type of the cookie sent from the client, these methods will automatically be called.

Implementation of Signed and Unsigned Cookie

Unsigned Cookie




const express = require('express');
const cookieParser = require('cookie-parser');
  
const app = express();
  
app.get('/', (req, res) => {
   res.cookie('name', 'GeeksForGeeks').send();
   console.log(req.cookies);
  
});
  
  
app.listen(3000, (err) => {
    if(err){ console.log(err) } 
    else { console.log('Success!!') }
});

The output for the above code will be –

"name=GeeksForGeeks"
Signed Cookie




var express = require('express')
var cookieParser = require('cookie-parser')
   
var app = express()
app.use(cookieParser('GFG'))
   
app.get('/', function (req, res) {
  res.cookie('name', 'GeeksForGeeks', { signed: true }).send();
  console.log(req.signedCookies)
})
   
app.listen(3000, (err) => {
  if(err) { console.log(err) }
  else { console.log('Success') }
})

Here, In the 4th line – “GFG” is provided as a secret value to the cookie.
In the 7th line – the name for the cookie is set to “GeeksForGeeks” and the object signed is set to true.
The output for the above code will be –

{ name: 'GeeksForGeeks' }

Article Tags :