Set get and delete cookie in Node JS; Through this tutorial, you will learn how to create/set, get and delete cookie in node js + express app.
Cookies are text files with small pieces of data — like a username and password — that are 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.
Set Get and Delete Cookie in Node JS
- 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/