Open In App
Related Articles

Express.js res.clearCookie() Function

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Report issue
Report

The res.clearCookie() function is used to clear the cookie specified by name. This function is called for clearing the cookies which as already been set. For example, if a user cookie is set, then it can be cleared using this function. 

Syntax:

res.clearCookie(name, [ options ])

Parameters:

  • Name: It is the name of the cookie which is to be cleared. Like in the following example, we have cleared the title cookie.
  • Options: It is an object that can have various properties like domain, encode, path, secure, etc.

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:

Filename: index.js 

javascript

const express = require('express');
const app = express();
const PORT = 3000;
 
app.get('/', function (req, res) {
 
    // Setting cookie (key-value)
    res.cookie('title', 'geeksforgeeks');
 
    // Clearing the cookie
    res.clearCookie('title');
 
    console.log("Cookie cleared");
});
 
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 module using the following command:

npm install express

Run the index.js file using the below command:

node index.js

Output:

Console Output:

Server listening on PORT 3000
Cookie cleared

Browser Output:

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

Server listening on PORT 3000

Last Updated : 16 Mar, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads