Skip to content
the poor coder

express js clear cookie

Poor Man
Published
Reading time
1 min read
On this page

If you are working with cookies in your Express.js application, you may need to clear a cookie at some point. Here's how you can do it:

Method 1: Using the response object#

The simplest way to clear a cookie is to set its value to an empty string and set the expiration date to a past date. You can do this using the response object's clearCookie method:

Text

  res.clearCookie('cookieName');
  

This will clear the cookie named "cookieName".

If you are using the cookie-parser middleware in your Express.js application, you can also clear a cookie using the clearCookie method on the request object:

Text

  req.clearCookie('cookieName');
  

This will also clear the cookie named "cookieName".

Method 3: Using the response object with options#

You can also clear a cookie by setting its value to an empty string and setting the expiration date to a past date, like in Method 1, but you can also pass in options as a third argument:

Text

  res.cookie('cookieName', '', {expires: new Date(0)});
  

This will set the cookie named "cookieName" to an empty string and set the expiration date to a past date. The options object can also include other properties like the domain, path, and secure flag.

Keep going

2023

how to sort an array

How to Sort an Array If you have an array and you want to order its elements in a specific way, you need to use a sorting algorithm. There are several sorting algorithms available, but two of the most commonly used are…

javascript2 min read
2023

jquery mobile or desktop

jQuery Mobile or Desktop? As a web developer, I have had some experience with both jQuery Mobile and jQuery Desktop. Both frameworks have their pros and cons, and which one to use really depends on the specific project…

javascript1 min read
2023

node.js folder structure

Node.js Folder Structure If you are new to Node.js, it can be daunting to create a folder structure that will work for your project. The structure you choose will depend on the size and complexity of your project. Here…

javascript1 min read
ESC