,

Using Cookies in Express JS

Posted by


In this tutorial, we will be looking at how to work with cookies in Express JS. Cookies are small pieces of data that are stored on the client’s browser. They are often used to store user-specific information, such as login details or preferences.

Step 1: Install and Set Up Express

Before we can start working with cookies in Express, we need to make sure we have Express installed. If you haven’t already installed Express, you can do so by running the following command in your terminal:

npm install express

After you have installed Express, create a new file and require Express in it:

const express = require('express');
const app = express();

Step 2: Setting a Cookie

To set a cookie in Express, we can use the res.cookie() method. This method takes three arguments: the name of the cookie, the value of the cookie, and optional options for the cookie (such as the expiration date).

Here’s an example of how to set a cookie in Express:

app.get('/setcookie', (req, res) => {
  res.cookie('username', 'john_doe', { maxAge: 900000, httpOnly: true });
  res.send('Cookie has been set');
});

In this example, we are setting a cookie named ‘username’ with the value ‘john_doe’ and an expiration date of 900,000 milliseconds (15 minutes). We have also set the httpOnly option to true, which means that the cookie is only accessible on the server-side and not on the client-side.

Step 3: Retrieving a Cookie

To retrieve a cookie in Express, we can use the req.cookies object. This object contains all the cookies that have been set in the current request. Here’s an example of how to retrieve a cookie in Express:

app.get('/getcookie', (req, res) => {
  const username = req.cookies.username;
  res.send(`Hello, ${username}`);
});

In this example, we are retrieving the value of the ‘username’ cookie from the req.cookies object and sending a response back to the client.

Step 4: Deleting a Cookie

To delete a cookie in Express, we can use the res.clearCookie() method. This method takes the name of the cookie as an argument. Here’s an example of how to delete a cookie in Express:

app.get('/deletecookie', (req, res) => {
  res.clearCookie('username');
  res.send('Cookie has been deleted');
});

In this example, we are deleting the ‘username’ cookie using the res.clearCookie() method.

Step 5: Secure Cookies

To make sure that cookies are secure, we can use the secure option when setting a cookie. This option ensures that the cookie is only sent over HTTPS connections. Here’s an example of how to set a secure cookie in Express:

app.get('/setsecurecookie', (req, res) => {
  res.cookie('secure_cookie', '123456', { secure: true });
  res.send('Secure cookie has been set');
});

In this example, we are setting a secure cookie named ‘secure_cookie’ with the value ‘123456’.

Overall, working with cookies in Express is straightforward and can be very useful for storing user-specific information. We have covered how to set, retrieve, and delete cookies in Express, as well as how to make cookies secure. I hope this tutorial has been helpful, and happy coding!

0 0 votes
Article Rating

Leave a Reply

7 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
@Olderaccount17
8 days ago

3 minutes in and I'm already dying for a platter of cookies.

@imsotoxd
8 days ago

what is this font? 😀

@xfactorbangs5821
8 days ago

Are you using the intelliSence for code Auto completion?,if not please reccomend what you are using .

@MeihaJensen
8 days ago

My takeaway

1) cookies are small piece of data that contains small information.

2) And it is set by the server and sent to the client, the client has to include the cookies upon subsequent request.

3) the cookie returned is in string format. And we’ll like it in JSON so we use cookieparser middleware.

4) cookie-parser middleware is useful for parsing cookies. To JSON format.

@three-zeros
8 days ago

Umm… I have a question: cookies will be deprecated and turned off in the Q2-Q3 of 2024 by Google, so can you use something else except them?

@josephkoh1562
8 days ago

can i use localstorage instead of cookie?

@zqgAFf
8 days ago

Hi I do have a question about your video I saw complete express courses, and I also saw those individual topic-based courses. But the complete one is older than the individual section. So would u please tell me the difference on contents

7
0
Would love your thoughts, please comment.x
()
x