Open In App

Express res.cookie() Function

Last Updated : 12 Apr, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

The res.cookie() function is used to set a cookie in the client’s browser. It allows you to assign a cookie by providing a name and a value. The value can be a simple string or an object, which will be automatically converted to JSON.

Syntax:

res.cookie(name, value [, options])
  • name: The name of the cookie.
  • value: The value of the cookie. It can be a string or an object that is converted to JSON.
  • options (Optional): An object that can contain properties like expires, domain, path, secure, etc. These help control the behavior of the cookie. 

Return Value: It returns an Object. 

Now, let’s move on the steps to demonstrate the use of res.cookie() function:

Steps to create the application:

Step 1: You can install this package by using this command.

npm install express

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

npm version express

Project Structure:

NodeProj

The updated dependencies in package.json file will look like:

"dependencies": {
    "express": "^5.1.0",
}

Example 1: Below is the code example of the res.cookie() Function.

JavaScript
const express = require('express');
const app = express();
const PORT = 3000;

app.get('/', function (req, res) {
    // Setting a simple cookie: 'name' = 'geeksforgeeks'
    res.cookie('name', 'geeksforgeeks');
    res.send("Cookie Set");
});

app.listen(PORT, function (err) {
    if (err) console.log(err);
    console.log("Server listening on PORT", PORT);
});

Steps to run the program:

node index.js

Output:

Server listening on PORT 3000

Visit http://localhost:3000/ to set a cookie named name with the value geeksforgeeks.

Example 2: Below is the code example of the res.cookie() Function.

JavaScript
const express = require('express');
const app = express();
const PORT = 3000;

// Middleware to set a cookie before handling the request
app.use('/', function (req, res, next) {
    res.cookie('title', 'GeeksforGeeks');
    res.send("Cookie Set");
    next();  // Pass control to the next handler
})

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: Visit http://localhost:3000/ to set a cookie named title with the value GeeksforGeeks.

Server listening on PORT 3000
Cookie SET

Conclusion

The res.cookie() function is simple to use and lets you set cookies in Express easily. While using middleware is possible, it’s not required just to set cookies, making the process straightforward for most applications.

We have a complete list of Express Response methods, properties and events, to check those please go through this Express Response Complete Reference article.



Next Article

Similar Reads