Skip to content
Related Articles

Related Articles

Express.js | res.clearCookie() Function

Improve Article
Save Article
  • Difficulty Level : Basic
  • Last Updated : 18 Jun, 2020
Improve Article
Save Article

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:

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

Installation of express module:

  1. You can visit the link to Install express module. You can install this package by using this command.
    npm install express
  2. After installing the express module, you can check your express version in command prompt using the command.
    npm version express
  3. 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

Filename: index.js




var express = require('express');
var app = express();
var 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:

  1. The project structure will look like this:
  2. Make sure you have installed express module using the following command:
    npm install express
  3. Run index.js file using below command:
    node index.js

    Output:

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

    Server listening on PORT 3000
    Cookie cleared
    
My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!