Serialize a Set-Cookie Header
Medium · Cookies
Express res.cookie() builds the Set-Cookie header with serialize() from the cookie package (and adds a few defaults of its own, like Path=/). Write serializeCookie(name, value, options) that works like cookie.serialize.
Start with name=value, where the value goes through encodeURIComponent. Then add attributes separated by "; ", in exactly this order, only when the option is set:
- maxAge (seconds) → Max-Age=<Math.floor(maxAge)>. Note that 0 is a real value (it deletes the cookie).
- domain → Domain=<domain>
- path → Path=<path>
- expires (an ISO date string) → Expires=<date.toUTCString()>
- httpOnly: true → HttpOnly
- secure: true → Secure
- sameSite ("strict", "lax" or "none", any casing) → SameSite=Strict, SameSite=Lax or SameSite=None