Open In App

Express.js req.cookies Property

Last Updated : 17 Mar, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

The req.cookies property is used when the user is using cookie-parser middleware. This property is an object that contains cookies sent by the request. 

Syntax:

req.cookies

Parameter: No parameters. 

Return Value: Object

Installation of the express module:

You can visit the link to Install the express module. You can install this package by using this command.

npm install express

After installing the express module, you can check your express version in the command prompt using the command.

npm version express

After that, you can just create a folder and add a file, for example, index.js. To run this file you need to run the following command.

node index.js

Project Structure:

Example 1: Filename: index.js 

javascript




const cookieParser = require('cookie-parser');
const express = require('express');
const app = express();
const PORT = 3000;
 
app.use(cookieParser());
 
app.get('/', function (req, res) {
    req.cookies.title = 'GeeksforGeeks';
    console.log(req.cookies);
    res.send();
});
 
app.listen(PORT, function (err) {
    if (err) console.log(err);
    console.log("Server listening on PORT", PORT);
});


Steps to run the program:

Make sure you have installed the express and cookie-parser module using the following command:

npm install express
npm install cookie-parser

Run the index.js file using the below command:

node index.js

Output:

Console Output:

Server listening on PORT 3000

Browser Output:

Now open your browser and go to http://localhost:3000/, now you can see the following output on your console:

Server listening on PORT 3000
[Object: null prototype] { title: 'GeeksforGeeks' }

Example 2: Filename: index.js 

javascript




const cookieParser = require('cookie-parser');
const express = require('express');
const app = express();
const PORT = 3000;
 
app.use(cookieParser());
 
app.get('/user', function (req, res) {
    req.cookies.name = 'Gourav';
    req.cookies.age = 12;
 
    console.log(req.cookies);
    res.send();
});
 
app.listen(PORT, function (err) {
    if (err) console.log(err);
    console.log("Server listening on PORT", PORT);
});


Steps to run the program:

Run the index.js file using the below command:

node index.js

Output: Now open your browser and make a GET request to http://localhost:3000/user, now you can see the following output on your console:

Server listening on PORT 3000
[Object: null prototype] { name: 'Gourav', age: 12 }

Reference: https://expressjs.com/en/4x/api.html#req.cookies



Previous Article
Next Article

Similar Reads

Difference between req.cookies and req.signedCookies in Express.js
req.cookies: Request. Cookies are supposed to be cookies that come from the client (browser) and Response. Cookies are cookies that will send back to the client (browser). Cookies are small files/data that are sent to the client with a server request and stored on the client side. This helps us to keep track of the user’s actions. Cookie-parser is
3 min read
Difference between req.query and req.params in Express
In this article, we will learn about the req.query and req.params and we will also learn the difference between them. Table of Content req.query: Dealing with URL Stuffreq.params: Figuring Out Route ThingsDifference between req.query and req.params in Express.jsreq.query: Dealing with URL Stuff:Where the Data Comes From: It's from the extra bits at
3 min read
Why are HTTP cookies used by Node.js for sending and receiving HTTP cookies?
The HTTP protocol is one of the most important protocols of the Application layer of the OSI model. The main use of this protocol is for sending hypertext information to the client to the server and server to the client to communicate on the World Wide Web. But, the HTTP protocol is a stateless protocol which means that this protocol cannot maintai
5 min read
Express.js req.subdomains Property
The req.subdomains property contains an array of subdomains in the domain name of the request. The application property subdomain offset, which defaults to 2 determines the beginning of the subdomain segments. Syntax: req.subdomains Parameter: No parameters. Returns: Array Installation of the express module: You can visit the link to Install the ex
2 min read
Express.js req.stale Property
The req.stale property indicates whether the incoming request is stale, and this property is the opposite of req.fresh property. Syntax: req.stale Parameter: No parameters. Return Value: True or False. Installation of the express module: You can visit the link to Install the express module. You can install this package by using this command. npm in
2 min read
Express.js req.secure Property
The req.secure property is a Boolean property that is true if a TLS connection is established else returns false. Syntax: req.secure Return Value: It returns a Boolean value either True or False. Installation of the express module: You can visit the link to Install the express module. You can install this package by using this command. npm install
2 min read
Express.js req.baseUrl Property
The req.baseUrl property is the URL path on which a router instance was mounted. The req.baseUrl property is similar to the mount path property of the app object, except app.mountpath returns the matched path pattern(s). Syntax: req.baseUrl Parameter: No parameters. Return Value: String Installation of the express module: You can visit the link to
2 min read
Express.js req.protocol Property
The req.protocol property contains the request protocol string which is either HTTP or (for TLS requests) https. When the trust proxy setting does not evaluate to false, this property will use the X-Forwarded-Proto header field value if it is present. Syntax: req.protocol Parameter: No parameters. Returns: String. Installation of the express module
2 min read
Express.js req.xhr Property
The req.xhr property returns a true value if the request’s X-Requested-With header field is XMLHttpRequest which indicates that the request was issued by a client library such as jQuery. Syntax: req.xhr Parameter: No parameters. Returns: True or False. Installation of the express module: You can visit the link to Install the express module. You can
2 min read
Express.js req.app Property
The req.app property holds the reference to the instance of the Express application that is using the middleware. Syntax: req.app Parameter: No parameters. Return Value: Object Installation of the express module: You can visit the link to Install the express module. You can install this package by using this command. npm install express After insta
2 min read