Set Get Delete Cookie in Node JS

Set get and delete cookie in Node JS; In this tutorial, i am going to show you how to create/set, get and delete cookie in node js + express app.

Node js Set Get Delete Cookie

  • Step 1 – Create Node Express js App
  • Step 2 – Install cookie-parser node module
  • Step 3 – Create/Set Cookie Route
  • Step 4 – Get/Fetch Cookie Route
  • Step 5 – Delete Cookie Route
  • Step 6 – Create App.js and Create Routes For Cookie
  • Step 7 – Start App Server

Step 1 – Create Node Express js App

Execute the following command on terminal to create node js app:

mkdir my-app
cd my-app
npm init -y

Step 2 – Install cookie-parser node module

Execute the following command on the terminal to install express and cookie parser modules:

npm install express cookie-parser --save

cookie-parser – cookie-parser is a middleware which parses cookies attached to the client request object. To use it, we will require it in our index. js file; this can be used the same way as we use other middleware.

Step 3 – Create/Set Cookie Route

Create/set cookie route in node js + express app:

// SET COOKIE
app.get('/set-cookie', (req, res) => {
    res.cookie('username', 'Webtutorials.ME', {
        maxAge: 1000 * 60, // 1 min
        httpOnly: true // http only, prevents JavaScript cookie access
    });
    // REDIRECT OT HOME
    res.redirect('/');
});

Step 4 – Get/Fetch Cookie Route

Get/fetch cookie route in node js + express app:

app.get('/', (req, res) => {
    let cookieVal = req.cookies.username;
    let show;
    
    if (cookieVal) {
        show = `Hi ${cookieVal} <br><a href="/delete-cookie">Delete Cookie</a>`;
    } else {
        show = `<a href="/set-cookie">Set Cookie</a><br>
        <a href="/delete-cookie">Delete Cookie</a><br>`;
    }
    res.send(show);
});

Step 5 – Delete Cookie Route

Delete/destroy cookie route in node js + express app:

// DELETE COOKIE
app.get('/delete-cookie', (req, res) => {
    //DELETING username COOKIE
    res.clearCookie('username');
    // REDIRECT OT HOME
    res.redirect('/');
});

Step 6 – Create App.js and Create Routes For Cookie

Visit your node js app directory and create app.js file. Then add the following get, set and delete cookie code into your app.js file:

const express = require('express');
const cookieParser = require('cookie-parser');
const app = express();
// APPLYING AS MIDDLEWARE
app.use(cookieParser());
app.get('/', (req, res) => {
    let cookieVal = req.cookies.username;
    let show;
    
    if (cookieVal) {
        show = `Hi ${cookieVal} <br><a href="/delete-cookie">Delete Cookie</a>`;
    } else {
        show = `<a href="/set-cookie">Set Cookie</a><br>
        <a href="/delete-cookie">Delete Cookie</a><br>`;
    }
    res.send(show);
});
// SET COOKIE
app.get('/set-cookie', (req, res) => {
    res.cookie('username', 'Webtutorials.ME', {
        maxAge: 1000 * 60, // 1 min
        httpOnly: true // http only, prevents JavaScript cookie access
    });
    // REDIRECT OT HOME
    res.redirect('/');
});
// DELETE COOKIE
app.get('/delete-cookie', (req, res) => {
    //DELETING username COOKIE
    res.clearCookie('username');
    // REDIRECT OT HOME
    res.redirect('/');
});
app.listen(3000, () => console.log('Your app listening on port 3000'));

Step 7 – Start App Server

You can use the following command to start node js app server:

//run the below command

npm start

after run this command open your browser and hit 

http://127.0.0.1:3000/

Recommended Node js Tutorial

Be First to Comment

Leave a Reply

Your email address will not be published.