How to Access HTTP Cookie in Node.js ?
Last Updated :
12 Jun, 2024
Cookies are small pieces of data sent by a server and stored on the client side, typically in the user's browser. They are often used to maintain stateful information such as user sessions, preferences, or tracking data. In Node.js, accessing and managing cookies is a common requirement for building web applications. This article will guide you through the process of accessing HTTP cookies in Node.js using popular modules like cookie-parser
and native methods.
Understanding HTTP Cookies
HTTP cookies are key-value pairs sent by the server to the client via HTTP headers. The client stores these cookies and sends them back to the server with subsequent requests. Cookies are set and read using the Set-Cookie
and Cookie
headers, respectively.
Using the cookie-parser
Middleware
The easiest way to access cookies in a Node.js application is by using the cookie-parser
middleware, which simplifies the process of parsing cookies from incoming requests.
Steps to Setup the Project
Step 1: Create a project folder and run the following command from the root directory of the project:
mkdir myapp
Step 2: Move to the current directory and initialize the node project.
cd myapp
npm init -y

Step 3: Install the necessary packages/libraries in your project using the following commands.
npm install express cookie-parser
Project Structure:

The updated dependencies in package.json file will look like:
"dependencies": {
"cookie": "^0.6.0",
"cookie-parser": "^1.4.6",
"express": "^4.19.2",
}
Example: Implementation to access HTTP Cookie in Node.js
Node
// Requiring modules
const express = require('express');
const cookieParser = require('cookie-parser');
const app = express();
// cookieParser middleware
app.use(cookieParser());
// Route for setting the cookies
app.get('/setcookie', function (req, res) {
// Setting a cookie with key 'my_cookie'
// and value 'geeksforgeeks'
res.cookie('my_cookie', 'geeksforgeeks');
res.send('Cookies added');
})
// Route for getting all the cookies
app.get('/getcookie', function (req, res) {
res.send(req.cookies);
})
// Server listens to port 3000
app.listen(3000, (err) => {
if (err) throw err;
console.log('server running on port 3000');
});
Explanation : Here we have a route /setcookie which is used to set a cookie with key my_cookie and the value as geeksforgeeks. We can alter these keys and values to be anything as per requirement. Another route is /getcookie which is used to get all the cookies and show them on the webpage. At the end of the code, we are listening to 3000 ports for our server to be able to run.
Steps to Run the Code:
node index.js

This will run the server as shown in the image above. We can check cookies by visiting localhost:3000/setcookie.

This will show a message as cookies are added. We can check the cookies by visiting localhost:3000/getcookie.

Now we can see that our cookies are added to the cookies object as shown above.
Similar Reads
How to Access Cache Data in Node.js ? Caching is an essential technique in software development that helps improve application performance and scalability by temporarily storing frequently accessed data. In Node.js, caching can significantly reduce database load, minimize API response times, and optimize resource utilization. This artic
5 min read
HTTP Cookies in Node.js Cookies are small data that are stored on a client side and sent to the client along with server requests. Cookies have various functionality, they can be used for maintaining sessions and adding user-specific features in your web app. For this, we will use cookie-parser module of npm which provides
5 min read
How to Remove a Cookie in PHP? Cookies are small text files stored in a user's browser. It contain data such as user preferences, session information, and other relevant details. PHP cookies are used to store information that can persist across different web pages or sessions. At times, you may need to delete or remove a cookie,
4 min read
How to Set Cookie in ReactJS ? Cookies in React are used to set value in a key-value form which can be set in browser storage and used further for identifying the current user. The following example shows how to set the cookie in the ReactJS application, here we have taken the custom-cookie as key, which is set in the cookie with
3 min read
How to access cookies in AngularJS? AngularJS is the most useful framework for developing dynamic web applications. While developing the applications, we need to store and access the cookies for many other functionalities like retiring the user details, etc. Cookies are nothing but the data that is usually stored in the client's brows
5 min read