Javatpoint Logo
Javatpoint Logo

Express.js Cookies Management

What are cookies

Cookies are small piece of information i.e. sent from a website and stored in user's web browser when user browses that website. Every time the user loads that website back, the browser sends that stored data back to website or server, to recognize user.

cookies in express.js

Install cookie

You have to acquire cookie abilities in Express.js. So, install cookie-parser middleware through npm by using the following command:

Express.js Cookies 1

Import cookie-parser into your app.

Define a route:

Cookie-parser parses Cookie header and populate req.cookies with an object keyed by the cookie names.

Let's define a new route in your express app like set a new cookie:

Browser sends back that cookie to the server, every time when it requests that website.

Express.js Cookies Example

File: cookies_example.js

ExpressJs cookies 2


Open the page on your browser:

ExpressJs cookies 3

Set cookie:

Now open to set the cookie:

ExpressJs cookies 4

Get cookie:

Now open to get the cookie:

ExpressJs cookies5

Youtube For Videos Join Our Youtube Channel: Join Now


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Trending Technologies

B.Tech / MCA