How To set, get and Delete Cookie Node Js?

30-Nov-2022

.

Admin

How To set, get and Delete Cookie Node Js?

Hello Friends,

I am going to explain to you an example of how to set. you'll learn to get and delete cookie node js. We will look at examples of how to delete cookie node js with code examples. you'll learn how to set. Here, Creating a basic example of a get and destroy cookie javascript node.

Cookies are text files with small pieces of data — like a username and password — that is used to identify your computer as you use a computer network. Specific cookies known as HTTP cookies are used to identify specific users and improve your web browsing experience.

Step 1: Create Node Express js App


Execute the following command on the terminal to create the 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 that 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 an 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 the 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/

I hope it can help you...

#Node JS