1

While creating back-end apps with express.js, I have used express-session and cookie-session.

But, recently I found out this cookie-parser package on npm. It's pretty popular.

My question is,

What is cookie-parser and how is it different from express-session or cookie-session ?

Rahul Dahal
  • 152
  • 1
  • 12

2 Answers2

2

Cookie Parser parses the incoming cookies from request to JSON value.

Whereas cookie-session or express-session is to maintain session on your server.

When your frontend sends a request, if cookies are set up, it will send some cookies based on usage, which by default are hard to interpret by server, so here cookie parser will parse those for easy understand ability. Which in turn could be used to create/maintain sessions or Authenticate (Depends upon cookie usage).

Shivam
  • 3,514
  • 2
  • 13
  • 27
1

First of all, we need to know a user session can be stored in two main ways with cookies: on the server or on the client.

  • cookie-session 2.0.0 - Stores the session data on the client within a cookie. Set and get cookies. Only use it when session data is relatively small and easily encoded as primitive values (rather than objects). Also, be aware that the cookie data will be visible to the client, so if there is any reason to keep it secure or obscure, then express-session may be a better choice.

  • express-session 1.17.3 - Stores only a session identifier on the client within a cookie and stores the session data on the server, typically in a database. Set and get cookies.

  • express-session <1.5.0 - It uses cookie-parser middleware to parse the cookie header and populate the req.cookies object. So we must use the cookie-parser middleware before session().

To store or access session data, simply use the request property req.session.

  • cookie-parser 1.4.6 - Parse Cookie header and populate req.cookies with an object keyed by the cookie names. Only get cookies.

All of them use cookies module in their latest version.

Lin Du
  • 88,126
  • 95
  • 281
  • 483