Socket
Socket
Sign inDemoInstall

cookie-parser

Package Overview
Dependencies
2
Maintainers
2
Versions
17
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    cookie-parser

cookie parsing with signatures


Version published
Weekly downloads
2.5M
decreased by-19.79%
Maintainers
2
Install size
18.4 kB
Created
Weekly downloads
 

Package description

What is cookie-parser?

The cookie-parser npm package is a middleware which parses cookies attached to the client request object. It can parse signed cookies with a secret and populate req.cookies with an object keyed by cookie names. It's commonly used in Express and Connect applications.

What are cookie-parser's main functionalities?

Parse Cookies

This code sets up an Express server that uses cookie-parser to parse cookies from the request. It logs the cookies to the console on a GET request to the root path.

const express = require('express');
const cookieParser = require('cookie-parser');

const app = express();
app.use(cookieParser());

app.get('/', (req, res) => {
  console.log('Cookies: ', req.cookies);
  res.send('Check the console for cookies');
});

app.listen(3000);

Parse Signed Cookies

This code demonstrates how to use cookie-parser to parse signed cookies. The secret provided to cookieParser() is used to validate the signed cookies, which are then available in req.signedCookies.

const express = require('express');
const cookieParser = require('cookie-parser');

const app = express();
app.use(cookieParser('yourSecret')); // Replace 'yourSecret' with your actual secret string

app.get('/', (req, res) => {
  console.log('Signed Cookies: ', req.signedCookies);
  res.send('Check the console for signed cookies');
});

app.listen(3000);

Other packages similar to cookie-parser

Readme

Source

Parse Cookie header and populate req.cookies with an object keyed by the cookie names. Optionally you may enabled signed cookie support by passing a secret string, which assigns req.secret so it may be used by other middleware.

Install

$ npm install cookie-parser

API

var cookieParser = require('cookie-parser')

cookieParser(secret, options)

  • secret a string used for signing cookies. This is optional and if not specified, will not parse signed cookies.
  • options an object that is passed to cookie.parse as the second option. See cookie for more information.
    • decode a funcction to decode the value of the cookie

Example

var cookieParser = require('cookie-parser');

connect()
 .use(cookieParser('optional secret string'))
 .use(function(req, res, next){
   res.end(JSON.stringify(req.cookies));
 })

License

MIT

Keywords

FAQs

Last updated on 12 May 2014

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc