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 install cookie-parser
yarn add cookie-parser
pnpm add cookie-parser
Import
import cookieParser from 'cookie-parser';
Quick Example
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
| Package | cookie-parser |
| Category | HTTP |
| Weekly Downloads | 5M+ |
| License | MIT |
| Install | npm install cookie-parser |
Related Packages
Express is the most widely used web application framework for Node.js, providing a minimal and flexi…
express-session is a session middleware for Express that creates server-side sessions identified by …
Body-parser is a Node.js body parsing middleware that extracts the body portion of incoming HTTP req…
CORS (Cross-Origin Resource Sharing) is an Express/Connect middleware that enables cross-origin requ…
Helmet helps secure Express.js applications by setting various HTTP response headers that protect ag…
Browse npm Packages by Category
Explore our reference of 200 popular npm packages with install commands, examples, and quick-start guides.