Languages
[Edit]
EN

Express.js - add cookie

0 points
Created by:
mkrieger1
726

In this short article, we would like to add cookie in Express.js.

1. Install cookie-parser package:

npm install cookie-parser

2. Use response.cookie() method to add cookies with specified cookie name and value.

Practical example

In this example, we use response.cookie() method to create username cookie wich stores john value.

const express = require('express')
const cookieParser = require('cookie-parser')
 
const app = express()

app.use(cookieParser())

app.get('/', (request, response){
    response.cookie('username', 'john').send('Cookie set');  // username=john
});

app.listen(3000);

 After running the code and entering localhost:3000, we will see the window below.

To check cookies on the website, we use Chrome DevTools (F12)
Use Chrome DevTools (F12) to check cookies on the website

See also

  1.  Express.js - delete cookie

Alternative titles

  1. Express.js - how to add cookie
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.
Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join