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 'w3wiki'
    res.cookie('my_cookie', 'w3wiki');
    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 w3wiki. 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.


How to Access HTTP Cookie in Node.js ?

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.

Similar Reads

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:...

Contact Us