How to set a cookie in Node js using the Express framework

0 votes

How to set a cookie in Node.js using the Express framework?

I’m building a web application with Node.js and Express, and I need to set a cookie on the client’s browser. I’ve heard about using the res.cookie method, but I’m unsure how to implement it correctly or configure options like expiration. Can someone guide me through the process?

Nov 26 in Web Development by Nidhi
• 4,940 points
67 views

1 answer to this question.

0 votes

You can use the res.cookie() method to set a cookie

Step 1: Install Express

If you don't have Express installed, add it to your project:

npm install express

Step 2: Basic Code to Set a Cookie

The res.cookie() method allows you to define a cookie with a name, value, and optional configurations.

const express = require('express');

const app = express();

app.get('/set-cookie', (req, res) => {

    // Setting a cookie with a name and value

    res.cookie('username', 'Mahak', {

        maxAge: 60000,     // 1 minute

        httpOnly: true,  // Makes the cookie inaccessible to JavaScript on the client side

        secure: false,   // Set true for HTTPS

        sameSite: 'strict' // Controls cross-site behavior

    });

    res.send('Cookie has been set!');

});


app.get('/get-cookie', (req, res) => {

    // Access cookies using req.cookies (requires cookie-parser)

    res.send(`Cookie value is: ${req.cookies?.username || 'cookie not found'}`);

});


app.listen(3000, () => {

    console.log('Server is running on http://localhost:3000');

});

answered Nov 27 by Navya

Related Questions In Web Development

0 votes
0 answers

How to run a function in a script from the command line in Node.js?

How to run a function in a ...READ MORE

Nov 27 in Web Development by Nidhi
• 4,940 points
51 views
0 votes
0 answers

How to upload a file to api server in node js?

How to upload a file to api ...READ MORE

Oct 14 in Web Development by anonymous
• 4,940 points
99 views
0 votes
0 answers

How To Implement Caching in Node.js Using Redis?

How To Implement Caching in Node.js Using ...READ MORE

Oct 21 in Web Development by Nidhi
• 4,940 points
114 views
0 votes
0 answers

How to upload a file to api server in node js?

How to upload a file to api ...READ MORE

Oct 21 in Web Development by Nidhi
• 4,940 points
145 views
0 votes
1 answer

Unable to start express server on AWS instance

It's not your code — you can't connect ...READ MORE

answered Oct 1, 2018 in AWS by Priyaj
• 58,020 points
3,176 views
0 votes
1 answer

Start script missing error when running npm start

It seems that there is an undefined ...READ MORE

answered Feb 10, 2022 in Java by Soham
• 9,710 points
4,478 views
0 votes
0 answers

Pre-rendering VS Server-side rendering for Angular SEO

i want to integrate an seo optimization ...READ MORE

Feb 14, 2022 in Others by Kichu
• 19,040 points
692 views
0 votes
1 answer

Pre-rendering VS Server-side rendering for Angular SEO

https://developers.google.com/web/updates/2019/02/rendering-on-the-web use this article it explains all about ...READ MORE

answered Feb 22, 2022 in Others by narikkadan
• 63,600 points
512 views
0 votes
1 answer
0 votes
1 answer
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP