npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

node-cookie

v2.1.2

Published

sign, encrypt and parse http cookies

Downloads

19,689

Readme

Node Cookie

Easily parse and write signed & encrypted cookies on Node.js HTTP requests.

NPM Version Build Status Appveyor Coveralls

node-cookie makes it simpler to create encrypted and signed cookies for HTTP requests.

You can use it with any framework or library of your choice.

See also

  1. node-req
  2. node-res

Basic Setup

const http = require('http')
const nodeCookie = require('node-cookie')

http.createServer(function (req, res) {

  // this will update set-cookie header on res object.
  nodeCookie.create(res, 'user', 'virk')

}).listen(3000)

Signing cookies with a secret

const http = require('http')
const nodeCookie = require('node-cookie')

http.createServer(function (req, res) {

  nodeCookie.create(res, 'user', 'virk', '16charlongsecret')

}).listen(3000)

Signing & encrypting cookies with a secret

const http = require('http')
const nodeCookie = require('node-cookie')

http.createServer(function (req, res) {

  nodeCookie.create(res, 'user', 'virk', '16charlongsecret', true)

}).listen(3000)

API

Cookie

Cookie parser is a simple utility module to read and write cookies on Node.js HTTP requests. It supports cookie signing and encryption.

parse(req, [secret], [decrypt]) ⇒ Object

Parses cookies from HTTP header Cookie into a javascript object. Also it will unsign and decrypt cookies encrypted and signed by this library using a secret.

Kind: inner method of Cookie

| Param | Type | Default | | --- | --- | --- | | req | http.IncomingRequest | | | [secret] | String | | | [decrypt] | Boolean | false |

Example

nodeCookie.parse(req)

// or if cookies were signed when writing
nodeCookie.parse(req, 'SECRET')

// also if cookies were encrypted
nodeCookie.parse(req, 'SECRET', true)

get(req, key, [secret], [decrypt], [cookies]) ⇒ Mixed

Returns value for a single cookie by its key. It is recommended to make use of this function when you want to pull a single cookie. Since the parse method will eagerly unsign and decrypt all the cookies.

Kind: inner method of Cookie

| Param | Type | Default | Description | | --- | --- | --- | --- | | req | http.IncomingRequest | | | | key | String | | | | [secret] | String | | | | [decrypt] | Boolean | false | | | [cookies] | Object | | Use existing cookies object over re-parsing them from the header. |

Example

nodeCookie.get(req, 'sessionId')

// if cookie was signed
nodeCookie.get(req, 'sessionId', 'SECRET')

// if cookie was encrypted
nodeCookie.get(req, 'sessionId', 'SECRET', true)

unPackValue(value, secret, decrypt) ⇒ String

Unpack cookie value by unsigning and decrypting it. Infact you can unpack any value packed via the packValue method.

Kind: inner method of Cookie

| Param | Type | | --- | --- | | value | String | | secret | String | | decrypt | Boolean |

packValue(value, [secret], [encrypt]) ⇒ String

Pack the value by properly formatting, signing and encrypting it.

Kind: inner method of Cookie

| Param | Type | Default | | --- | --- | --- | | value | String | | | [secret] | String | | | [encrypt] | Boolean | false |

create(res, key, value, [options], [secret], [encrypt]) ⇒ void

Write cookie to the HTTP response object. It will append duplicate cookies to the Set-Cookie header, since browsers discard the duplicate cookies by themselves

Kind: inner method of Cookie

| Param | Type | Default | | --- | --- | --- | | res | http.ServerResponse | | | key | String | | | value | * | | | [options] | Object | {} | | [secret] | String | | | [encrypt] | Boolean | false |

Example

nodeCookie.create(res, 'sessionId', 1)

// sign session id
nodeCookie.create(res, 'sessionId', 1, {}, 'SECRET')

// sign and encrypt session id
nodeCookie.create(res, 'sessionId', 1, {}, 'SECRET', true)

clear(res, key, [options]) ⇒ void

Clears the cookie from browser by setting it's expiry in past. This is required since there is no other way to instruct the browser to delete a cookie.

Also this method will override the expires value on the options object.

Kind: inner method of Cookie

| Param | Type | Default | | --- | --- | --- | | res | http.ServerResponse | | | key | String | | | [options] | Object | {} |

Example

nodeCookie.clear(res, 'sessionId')