🌐
HTTP5M+/wkMIT

cookie-parser

Cookie-parser is an Express middleware that parses Cookie header values and populates req.cookies with an object keyed by cookie names. It also supports parsing

Installation

npm
npm install cookie-parser
yarn
yarn add cookie-parser
pnpm
pnpm add cookie-parser

Import

ESM
import cookieParser from 'cookie-parser';

Quick Example

usage
import express from 'express';
import cookieParser from 'cookie-parser';

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

app.get('/', (req, res) => {
  console.log(req.cookies);
  res.cookie('visited', 'true');
  res.send('Cookie set');
});

About cookie-parser

Cookie-parser is an Express middleware that parses Cookie header values and populates req.cookies with an object keyed by cookie names. It also supports parsing signed cookies using a secret string or array of secrets, placing verified signed cookies in req.signedCookies and setting unsigned or tampered cookies to false. The middleware uses the cookie npm package under the hood for parsing the raw Cookie header string according to RFC 6265. Cookie-parser handles URL-encoded cookie values, JSON-encoded cookie values (prefixed with j:), and signed cookie values (prefixed with s:) transparently. The middleware is essential for Express applications that need to read cookies set by the client for session management, user preferences, tracking, or authentication tokens. While Express can set cookies using res.cookie() without any middleware, reading incoming cookies requires either cookie-parser or manual header parsing. Cookie-parser is lightweight, adding minimal overhead to request processing. For modern applications that use JWT tokens in Authorization headers instead of cookies, cookie-parser may not be necessary, but it remains essential for traditional session-based authentication patterns.

Quick Facts

Packagecookie-parser
CategoryHTTP
Weekly Downloads5M+
LicenseMIT
Installnpm install cookie-parser

Related Packages

Browse npm Packages by Category

Explore our reference of 200 popular npm packages with install commands, examples, and quick-start guides.