Basic HTTP cookie parser and serializer for HTTP servers.
$ npm install cookieconst cookie = require("cookie");
// import * as cookie from 'cookie';Alias: parse(str, options) (deprecated)
Parse an HTTP Cookie header string and return an object of all cookie name-value pairs.
The str argument is the string representing a Cookie header value and options is an
optional object containing additional parsing options.
const cookieObject = cookie.parseCookie("foo=bar; equation=E%3Dmc%5E2");
// { foo: 'bar', equation: 'E=mc^2' }decodeSpecifies the function to decode a cookie-value. Defaults todecodeURIComponent.
Stringifies a cookie object into an HTTP Cookie header.
const cookieHeader = cookie.stringifyCookie({ a: "foo", b: "bar" });
// a=foo; b=barencodeSpecifies the function to encode a cookie-value. Defaults toencodeURIComponent.
Parse an HTTP Set-Cookie header string and return an object of the options.
const setCookieObject = cookie.parseSetCookie("foo=bar; httpOnly");
// { name: "foo", value: "bar", httpOnly: true }Note: Cookie follows the specification and ignores invalid attributes, but does not attempt to normalize or modify any attributes as a browser might. For example:
cookie.parseSetCookie(
"session=abc; max-age=1.5; expires=invalid; custom=value; domain=example.com",
);
// { name: "session", value: "abc", domain: "example.com" }decodeSpecifies the function to decode a cookie-value. Defaults todecodeURIComponent.
Alias: serialize(str, val, options) (deprecated)
Stringifies a Set-Cookie object into a Set-Cookie header string.
const setCookieHeader = cookie.stringifySetCookie({
name: "foo",
value: "bar",
});
// foo=barencodeSpecifies the function to encode a cookie-value. Defaults toencodeURIComponent.
The cookie object represents all cookie name-value pairs in a Cookie header, where { name: "value" } is used for name=value.
The Set-Cookie object represents all the options in a Set-Cookie header.
The name of the cookie.
The value of a cookie after it has been decoded.
Specifies the number (in seconds) to be the value for the Max-Age Set-Cookie attribute.
The cookie storage model specification states that if both expires and
maxAge are set, then maxAge takes precedence, but it is possible not all clients by obey this,
so if both are set, they should point to the same date and time.
Specifies the Date object to be the value for the Expires Set-Cookie attribute.
When no expiration is set, clients consider this a "non-persistent cookie" and delete it when the current session is over.
The cookie storage model specification states that if both expires and
maxAge are set, then maxAge takes precedence, but it is possible not all clients by obey this,
so if both are set, they should point to the same date and time.
Specifies the value for the Domain Set-Cookie attribute.
When no domain is set, clients consider the cookie to apply to the current domain only.
Specifies the value for the Path Set-Cookie attribute.
When no path is set, the path is considered the "default path".
Enables the HttpOnly Set-Cookie attribute.
When enabled, clients will not allow client-side JavaScript to see the cookie in document.cookie.
Enables the Secure Set-Cookie attribute.
When enabled, clients will only send the cookie back if the browser has an HTTPS connection.