Languages
[Edit]
EN

Node.js / Express.js - add cookie

0 points
Created by:
mkrieger1
516

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

Note:

To be able to work with cookies in Express.js, we will use cookie-parser middleware, so use:

npm install cookie-parser

 Check this article to learn how to delete cookie - link

Setting cookie example

To add cookies, we use response.cookie('cookie name', 'cookie value') method.

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
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