How to manipulate cookies by using ‘Response.cookie()’ in Express?
Last Updated :
19 Dec, 2023
Cookies are small data that is stored on the client’s computer. Using this cookie various tasks like authentication, session management, etc can be done. In Express JS we can use the cookie-parser middleware to manage the cookies in the application. In this article, we going to manipulate cookies in Express JS by using the two scenarios/approaches below.
Prerequisites
The two ways we can manipulate the cookies are discussed below:
Approach 1: Updating Cookie Value
In this apporach, we will modify the existing cookie information that is stored in the user’s browser. By using the res.cookie() method in Express.js, we will set a new value for the cookie
Syntax:
res.cookie('cookieName', 'newCookieValue');
Example: In the below example, firstly we have defined the route /setCookie which sets the cookie value as “Geek“. Then we update the cookie value in the route /updateCookie by using the res.cookie() method. We have updated the cookie value from “Geek” to “GeeksforGeeks“.
Javascript
const express = require( 'express' );
const cookieParser = require( 'cookie-parser' );
const app = express();
app.use(cookieParser());
app.get( '/setCookie' , (req, res) => {
res.cookie( 'user' , 'Geek' );
res.send( 'Cookie has been set!' );
});
app.get( '/updateCookie' , (req, res) => {
const currUserValue = req.cookies.user;
if (!currUserValue) {
return res.status(400).send( 'Please set the cookie first by visiting /setCookie' );
}
res.cookie( 'user' , 'GeeksforGeeks' );
res.send( 'Cookie has been updated!' );
});
app.listen(3000, () => {
console.log( 'Server is running on port 3000' );
});
|
Output:
Approach 2: Deleting Cookie Value
In this apporach, we will delete the cookie which is already set in the Express.js application. Firstly, we define the route to set the cookie and then we will perform the deletion of the cookie using the res.cookie() method.
Syntax:
res.cookie('cookieName', '', { expires: new Date(0) });
Example: In the below example, we have set the route /setCookie to set the cookie as “Geek“, then we have set another new route as /deleteCookie to delete the cookie which was set. For deleting the cookie, we are passing an empty value wit the immediate expiration in the res.cookie() method.
Javascript
const express = require( 'express' );
const cookieParser = require( 'cookie-parser' );
const app = express();
app.use(cookieParser());
app.get( '/setCookie' , (req, res) => {
res.cookie( 'user' , 'Geek' );
res.send( 'Cookie has been set!' );
});
app.get( '/deleteCookie' , (req, res) => {
const currUserValue = req.cookies.user;
if (!currUserValue) {
return res.status(400).send( 'Please set the cookie first by visiting /setCookie' );
}
res.cookie( 'user' , '' , { expires: new Date(0) });
res.send( 'Cookie has been deleted!' );
});
a
pp.listen(3000, () => {
console.log( 'Server is running on port 3000' );
});
|
Output:
Conclusion
Managing the cookies in the Express JS application is an important task as the cookies consist of information that is used for various takes including authentication. The middleware cookie parser in Express JS makes the tasks of cookie modification easier in the Express JS applications.
Share your thoughts in the comments
Please Login to comment...