\href{https://npmjs.org/package/cookie}{\texttt{ }} \href{https://npmjs.org/package/cookie}{\texttt{ }} \href{https://nodejs.org/en/download}{\texttt{ }} \href{https://travis-ci.org/jshttp/cookie}{\texttt{ }} \href{https://coveralls.io/r/jshttp/cookie?branch=master}{\texttt{ }}

Basic HTTP cookie parser and serializer for HTTP servers.\hypertarget{md_sfcode_node_modules_engine_io_node_modules_cookie_README_autotoc_md8434}{}\doxysection{Installation}\label{md_sfcode_node_modules_engine_io_node_modules_cookie_README_autotoc_md8434}

\begin{DoxyCode}{0}
\DoxyCodeLine{\$ npm install cookie }

\end{DoxyCode}
\hypertarget{md_sfcode_node_modules_engine_io_node_modules_cookie_README_autotoc_md8435}{}\doxysection{API}\label{md_sfcode_node_modules_engine_io_node_modules_cookie_README_autotoc_md8435}

\begin{DoxyCode}{0}
\DoxyCodeLine{var cookie = require('cookie'); }

\end{DoxyCode}
\hypertarget{md_sfcode_node_modules_engine_io_node_modules_cookie_README_autotoc_md8436}{}\doxysubsection{cookie.\+parse(str, options)}\label{md_sfcode_node_modules_engine_io_node_modules_cookie_README_autotoc_md8436}
Parse an HTTP {\ttfamily Cookie} header string and returning an object of all cookie name-\/value pairs. The {\ttfamily str} argument is the string representing a {\ttfamily Cookie} header value and {\ttfamily options} is an optional object containing additional parsing options.


\begin{DoxyCode}{0}
\DoxyCodeLine{var cookies = cookie.parse('foo=bar; equation=E\%3Dmc\%5E2');}
\DoxyCodeLine{// \{ foo: 'bar', equation: 'E=mc\string^2' \} }

\end{DoxyCode}
\hypertarget{md_sfcode_node_modules_engine_io_node_modules_cookie_README_autotoc_md8437}{}\doxysubsubsection{Options}\label{md_sfcode_node_modules_engine_io_node_modules_cookie_README_autotoc_md8437}
{\ttfamily cookie.\+parse} accepts these properties in the options object.\hypertarget{md_sfcode_node_modules_engine_io_node_modules_cookie_README_autotoc_md8438}{}\doxyparagraph{decode}\label{md_sfcode_node_modules_engine_io_node_modules_cookie_README_autotoc_md8438}
Specifies a function that will be used to decode a cookie\textquotesingle{}s value. Since the value of a cookie has a limited character set (and must be a simple string), this function can be used to decode a previously-\/encoded cookie value into a Java\+Script string or other object.

The default function is the global {\ttfamily decode\+URIComponent}, which will decode any URL-\/encoded sequences into their byte representations.

{\bfseries{note}} if an error is thrown from this function, the original, non-\/decoded cookie value will be returned as the cookie\textquotesingle{}s value.\hypertarget{md_sfcode_node_modules_engine_io_node_modules_cookie_README_autotoc_md8439}{}\doxysubsection{cookie.\+serialize(name, value, options)}\label{md_sfcode_node_modules_engine_io_node_modules_cookie_README_autotoc_md8439}
Serialize a cookie name-\/value pair into a {\ttfamily Set-\/\+Cookie} header string. The {\ttfamily name} argument is the name for the cookie, the {\ttfamily value} argument is the value to set the cookie to, and the {\ttfamily options} argument is an optional object containing additional serialization options.


\begin{DoxyCode}{0}
\DoxyCodeLine{var setCookie = cookie.serialize('foo', 'bar');}
\DoxyCodeLine{// foo=bar }

\end{DoxyCode}
\hypertarget{md_sfcode_node_modules_engine_io_node_modules_cookie_README_autotoc_md8440}{}\doxysubsubsection{Options}\label{md_sfcode_node_modules_engine_io_node_modules_cookie_README_autotoc_md8440}
{\ttfamily cookie.\+serialize} accepts these properties in the options object.\hypertarget{md_sfcode_node_modules_engine_io_node_modules_cookie_README_autotoc_md8441}{}\doxyparagraph{domain}\label{md_sfcode_node_modules_engine_io_node_modules_cookie_README_autotoc_md8441}
Specifies the value for the \href{https://tools.ietf.org/html/rfc6266\#section-5.2.3}{\texttt{ {\ttfamily Domain} {\ttfamily Set-\/\+Cookie} attribute}}. By default, no domain is set, and most clients will consider the cookie to apply to only the current domain.\hypertarget{md_sfcode_node_modules_engine_io_node_modules_cookie_README_autotoc_md8442}{}\doxyparagraph{encode}\label{md_sfcode_node_modules_engine_io_node_modules_cookie_README_autotoc_md8442}
Specifies a function that will be used to encode a cookie\textquotesingle{}s value. Since value of a cookie has a limited character set (and must be a simple string), this function can be used to encode a value into a string suited for a cookie\textquotesingle{}s value.

The default function is the global {\ttfamily ecode\+URIComponent}, which will encode a Java\+Script string into UTF-\/8 byte sequences and then URL-\/encode any that fall outside of the cookie range.\hypertarget{md_sfcode_node_modules_engine_io_node_modules_cookie_README_autotoc_md8443}{}\doxyparagraph{expires}\label{md_sfcode_node_modules_engine_io_node_modules_cookie_README_autotoc_md8443}
Specifies the {\ttfamily Date} object to be the value for the \href{https://tools.ietf.org/html/rfc6266\#section-5.2.1}{\texttt{ {\ttfamily Expires} {\ttfamily Set-\/\+Cookie} attribute}}. By default, no expiration is set, and most clients will consider this a \char`\"{}non-\/persistent cookie\char`\"{} and will delete it on a condition like exiting a web browser application.

{\bfseries{note}} the \href{https://tools.ietf.org/html/rfc6266\#section-5.3}{\texttt{ cookie storage model specification}} states that if both {\ttfamily expires} and {\ttfamily mag\+Age} are set, then {\ttfamily max\+Age} takes precedence, but it is possiblke not all clients by obey this, so if both are set, they should point to the same date and time.\hypertarget{md_sfcode_node_modules_engine_io_node_modules_cookie_README_autotoc_md8444}{}\doxyparagraph{http\+Only}\label{md_sfcode_node_modules_engine_io_node_modules_cookie_README_autotoc_md8444}
Specifies the {\ttfamily boolean} value for the \mbox{[}{\ttfamily Http\+Only} {\ttfamily Set-\/\+Cookie} attribute\mbox{]}\mbox{[}rfc-\/6266-\/5.\+2.\+6\mbox{]}. When truthy, the {\ttfamily Http\+Only} attribute is set, otherwise it is not. By default, the {\ttfamily Http\+Only} attribute is not set.

{\bfseries{note}} be careful when setting this to {\ttfamily true}, as compliant clients will not allow client-\/side Java\+Script to see the cookie in {\ttfamily document.\+cookie}.\hypertarget{md_sfcode_node_modules_engine_io_node_modules_cookie_README_autotoc_md8445}{}\doxyparagraph{max\+Age}\label{md_sfcode_node_modules_engine_io_node_modules_cookie_README_autotoc_md8445}
Specifies the {\ttfamily number} (in seconds) to be the value for the \href{https://tools.ietf.org/html/rfc6266\#section-5.2.2}{\texttt{ {\ttfamily Max-\/\+Age} {\ttfamily Set-\/\+Cookie} attribute}}. The given number will be converted to an integer by rounding down. By default, no maximum age is set.

{\bfseries{note}} the \href{https://tools.ietf.org/html/rfc6266\#section-5.3}{\texttt{ cookie storage model specification}} states that if both {\ttfamily expires} and {\ttfamily mag\+Age} are set, then {\ttfamily max\+Age} takes precedence, but it is possiblke not all clients by obey this, so if both are set, they should point to the same date and time.\hypertarget{md_sfcode_node_modules_engine_io_node_modules_cookie_README_autotoc_md8446}{}\doxyparagraph{path}\label{md_sfcode_node_modules_engine_io_node_modules_cookie_README_autotoc_md8446}
Specifies the value for the \href{https://tools.ietf.org/html/rfc6266\#section-5.2.4}{\texttt{ {\ttfamily Path} {\ttfamily Set-\/\+Cookie} attribute}}. By default, the path is considered the \href{https://tools.ietf.org/html/rfc6266\#section-5.1.4}{\texttt{ \char`\"{}default path\char`\"{}}}. By default, no maximum age is set, and most clients will consider this a \char`\"{}non-\/persistent cookie\char`\"{} and will delete it on a condition like exiting a web browser application.\hypertarget{md_sfcode_node_modules_engine_io_node_modules_cookie_README_autotoc_md8447}{}\doxyparagraph{same\+Site}\label{md_sfcode_node_modules_engine_io_node_modules_cookie_README_autotoc_md8447}
Specifies the {\ttfamily boolean} or {\ttfamily string} to be the value for the \href{https://tools.ietf.org/html/draft-west-first-party-cookies-07}{\texttt{ {\ttfamily Same\+Site} {\ttfamily Set-\/\+Cookie} attribute}}.


\begin{DoxyItemize}
\item {\ttfamily true} will set the {\ttfamily Same\+Site} attribute to {\ttfamily Strict} for strict same site enforcement.
\item {\ttfamily false} will not set the {\ttfamily Same\+Site} attribute.
\item `\textquotesingle{}lax'{\ttfamily will set the}Same\+Site{\ttfamily attribute to}Lax{\ttfamily for lax same site enforcement. -\/}\textquotesingle{}strict\textquotesingle{}{\ttfamily will set the}Same\+Site{\ttfamily attribute to}Strict\`{} for strict same site enforcement.
\end{DoxyItemize}

More information about the different enforcement levels can be found in the specification \href{https://tools.ietf.org/html/draft-west-first-party-cookies-07\#section-4.1.1}{\texttt{ https\+://tools.\+ietf.\+org/html/draft-\/west-\/first-\/party-\/cookies-\/07\#section-\/4.\+1.\+1}}

{\bfseries{note}} This is an attribute that has not yet been fully standardized, and may change in the future. This also means many clients may ignore this attribute until they understand it.\hypertarget{md_sfcode_node_modules_engine_io_node_modules_cookie_README_autotoc_md8448}{}\doxyparagraph{secure}\label{md_sfcode_node_modules_engine_io_node_modules_cookie_README_autotoc_md8448}
Specifies the {\ttfamily boolean} value for the \mbox{[}{\ttfamily Secure} {\ttfamily Set-\/\+Cookie} attribute\mbox{]}\mbox{[}rfc-\/6266-\/5.\+2.\+5\mbox{]}. When truthy, the {\ttfamily Secure} attribute is set, otherwise it is not. By default, the {\ttfamily Secure} attribute is not set.

{\bfseries{note}} be careful when setting this to {\ttfamily true}, as compliant clients will not send the cookie back to the server in the future if the browser does not have an HTTPS connection.\hypertarget{md_sfcode_node_modules_engine_io_node_modules_cookie_README_autotoc_md8449}{}\doxysection{Example}\label{md_sfcode_node_modules_engine_io_node_modules_cookie_README_autotoc_md8449}
The following example uses this module in conjunction with the Node.\+js core HTTP server to prompt a user for their name and display it back on future visits.


\begin{DoxyCode}{0}
\DoxyCodeLine{var cookie = require('cookie');}
\DoxyCodeLine{var escapeHtml = require('escape-\/html');}
\DoxyCodeLine{var http = require('http');}
\DoxyCodeLine{var url = require('url');}
\DoxyCodeLine{}
\DoxyCodeLine{function onRequest(req, res) \{}
\DoxyCodeLine{  // Parse the query string}
\DoxyCodeLine{  var query = url.parse(req.url, true, true).query;}
\DoxyCodeLine{}
\DoxyCodeLine{  if (query \&\& query.name) \{}
\DoxyCodeLine{    // Set a new cookie with the name}
\DoxyCodeLine{    res.setHeader('Set-\/Cookie', cookie.serialize('name', String(query.name), \{}
\DoxyCodeLine{      httpOnly: true,}
\DoxyCodeLine{      maxAge: 60 * 60 * 24 * 7 // 1 week}
\DoxyCodeLine{    \}));}
\DoxyCodeLine{}
\DoxyCodeLine{    // Redirect back after setting cookie}
\DoxyCodeLine{    res.statusCode = 302;}
\DoxyCodeLine{    res.setHeader('Location', req.headers.referer || '/');}
\DoxyCodeLine{    res.end();}
\DoxyCodeLine{    return;}
\DoxyCodeLine{  \}}
\DoxyCodeLine{}
\DoxyCodeLine{  // Parse the cookies on the request}
\DoxyCodeLine{  var cookies = cookie.parse(req.headers.cookie || '');}
\DoxyCodeLine{}
\DoxyCodeLine{  // Get the visitor name set in the cookie}
\DoxyCodeLine{  var name = cookies.name;}
\DoxyCodeLine{}
\DoxyCodeLine{  res.setHeader('Content-\/Type', 'text/html; charset=UTF-\/8');}
\DoxyCodeLine{}
\DoxyCodeLine{  if (name) \{}
\DoxyCodeLine{    res.write('<p>Welcome back, <b>' + escapeHtml(name) + '</b>!</p>');}
\DoxyCodeLine{  \} else \{}
\DoxyCodeLine{    res.write('<p>Hello, new visitor!</p>');}
\DoxyCodeLine{  \}}
\DoxyCodeLine{}
\DoxyCodeLine{  res.write('<form method="{}GET"{}>');}
\DoxyCodeLine{  res.write('<input placeholder="{}enter your name"{} name="{}name"{}> <input type="{}submit"{} value="{}Set Name"{}>');}
\DoxyCodeLine{  res.end('</form');}
\DoxyCodeLine{\}}
\DoxyCodeLine{}
\DoxyCodeLine{http.createServer(onRequest).listen(3000); }

\end{DoxyCode}
\hypertarget{md_sfcode_node_modules_engine_io_node_modules_cookie_README_autotoc_md8450}{}\doxysection{Testing}\label{md_sfcode_node_modules_engine_io_node_modules_cookie_README_autotoc_md8450}

\begin{DoxyCode}{0}
\DoxyCodeLine{\$ npm test }

\end{DoxyCode}
\hypertarget{md_sfcode_node_modules_engine_io_node_modules_cookie_README_autotoc_md8451}{}\doxysection{References}\label{md_sfcode_node_modules_engine_io_node_modules_cookie_README_autotoc_md8451}

\begin{DoxyItemize}
\item \href{https://tools.ietf.org/html/rfc6266}{\texttt{ RFC 6266\+: HTTP State Management Mechanism}}
\item \href{https://tools.ietf.org/html/draft-west-first-party-cookies-07}{\texttt{ Same-\/site Cookies}}
\end{DoxyItemize}\hypertarget{md_sfcode_node_modules_engine_io_node_modules_cookie_README_autotoc_md8452}{}\doxysection{License}\label{md_sfcode_node_modules_engine_io_node_modules_cookie_README_autotoc_md8452}
\mbox{[}MIT\mbox{]}(LICENSE) 