I feel quite confused, do not understand what is the difference between res.cookie and req.cookies. And more strangely, I have found that if I do not set a cookie:
//The value will be:
req.cookies.uid=="undefined"
//instead of:
req.cookies.uid==undefined
Why the express.js design the cookie like this?
If I want to implement a “remember me” function while users trying to log in and set the cookie expire time to infinite or maybe a year, how should I use the cookie correctly?
I have found that the cookieParser only support thing like this:
express.cookieParser("secret")
And does not support expire/maxAge setting.
res.cookieis actually a function with a signature ofres.cookie(key, value, opts). You can use it to set a client’s cookie values/options. On the other hand,req.cookiesis an object that gives you the client’s current cookie values. Here’s an example using cookies to track page views:If you use the
express.cookieSession()middleware, you can set application wide default cookie properties. For example, the cookie’smaxAgeproperty determines how many milliseconds in the future the cookie will expire, so here I set it to expire in one hour:Otherwise, you could set the cookie options individually by passing an options object to
res.cookie().