CookieParser vs req.cookies

When I read about req.cookies in expressjs docs, it says

When the cookieParser() middleware is used this object defaults to {}, otherwise contains the cookies sent by the user-agent.

And when I read about CookieParser middleware, it says

Parses the Cookie header field and populates req.cookies with an object keyed by the cookie names.

So, req.cookies says if I use CookieParser, it will be set to {}, but CookieParser says it will populate req.cookies. Sounds confusing to me.

My question is, why do we need CookieParser, if req.cookies itself gives the cookies? I asked this question in stackoverflow also.

Author: Fantashit

1 thought on “CookieParser vs req.cookies

  1. @billinghamj I agree that at first glance it seems odd. But parsing cookies is a perf hit on every request when you don’t need it, and setting cookies is just setting a header. I do imagine that moving out that functionality so it is an optional mixin to the response object (via middleware or something else) could be a good way forward for 5.x.

Comments are closed.