|
||
---|---|---|
.. | ||
index.js | ||
LICENSE | ||
package.json | ||
README.md |
cookie
[![NPM Version][npm-version-image]][npm-url] [![NPM Downloads][npm-downloads-image]][npm-url] [![Node.js Version][node-image]][node-url] [![Build Status][ci-image]][ci-url] [![Coverage Status][coveralls-image]][coveralls-url]
Basic HTTP cookie parser and serializer for HTTP servers.
Installation
This is a Node.js module available through the
npm registry. Installation is done using the
npm install
command:
$ npm install cookie
API
var cookie = require('cookie');
cookie.parse(str, options)
Parse an HTTP Cookie
header string and returning 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.
var cookies = cookie.parse('foo=bar; equation=E%3Dmc%5E2');
// { foo: 'bar', equation: 'E=mc^2' }
Options
cookie.parse
accepts these properties in the options object.
decode
Specifies a function that will be used to decode a cookie'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 JavaScript string or other object.
The default function is the global decodeURIComponent
, which will decode any URL-encoded
sequences into their byte representations.
note if an error is thrown from this function, the original, non-decoded cookie value will be returned as the cookie's value.
cookie.serialize(name, value, options)
Serialize a cookie name-value pair into a Set-Cookie
header string. The name
argument is the
name for the cookie, the value
argument is the value to set the cookie to, and the options
argument is an optional object containing additional serialization options.
var setCookie = cookie.serialize('foo', 'bar');
// foo=bar
Options
cookie.serialize
accepts these properties in the options object.
domain
Specifies the value for the [Domain
Set-Cookie
attribute][rfc-6265-5.2.3]. By default, no
domain is set, and most clients will consider the cookie to apply to only the current domain.
encode
Specifies a function that will be used to encode a cookie'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's value.
The default function is the global encodeURIComponent
, which will encode a JavaScript string
into UTF-8 byte sequences and then URL-encode any that fall outside of the cookie range.
expires
Specifies the Date
object to be the value for the [Expires
Set-Cookie
attribute][rfc-6265-5.2.1].
By default, no expiration is set, and most clients will consider this a "non-persistent cookie" and
will delete it on a condition like exiting a web browser application.
note the [cookie storage model