Node.js / Express.js - add cookie

0 points
Created by:

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


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.get('/', (request, response){
   response.cookie('username', 'john').send('Cookie set');  // username=john


 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

Alternative titles

  1. Node.js / 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.

❤️💻 🙂