express-session
express-session is a session middleware for Express that creates server-side sessions identified by a session ID cookie sent to the client. When a user makes a …
Installation
npm install express-session
yarn add express-session
pnpm add express-session
Import
import session from 'express-session';
Quick Example
import express from 'express';
import session from 'express-session';
const app = express();
app.use(session({
secret: process.env.SESSION_SECRET,
resave: false,
saveUninitialized: false,
cookie: { secure: true, maxAge: 86400000 },
}));
app.get('/', (req, res) => {
req.session.views = (req.session.views || 0) + 1;
res.send(`Views: ${req.session.views}`);
});About express-session
express-session is a session middleware for Express that creates server-side sessions identified by a session ID cookie sent to the client. When a user makes a request, the middleware reads the session ID from the cookie, retrieves the corresponding session data from a store, and makes it available at req.session. Session data persists across requests for the same user, enabling authentication state, shopping carts, form wizard progress, and user preferences without client-side storage. The middleware supports configurable cookie options (name, maxAge, httpOnly, secure, sameSite, domain, path), session regeneration for security (preventing session fixation attacks), and rolling sessions that reset the cookie expiration on each request. By default, express-session uses an in-memory store suitable only for development — production deployments should use persistent stores like connect-redis, connect-mongo, connect-pg-simple, or connect-session-sequelize for session data. The middleware integrates with Passport for authentication session management and supports session destruction for logout functionality. express-session is essential for traditional server-rendered applications that use cookie-based authentication, though modern SPAs often prefer stateless JWT-based approaches instead.
Quick Facts
| Package | express-session |
| Category | Auth |
| Weekly Downloads | 2M+ |
| License | MIT |
| Install | npm install express-session |
Related Packages
Express is the most widely used web application framework for Node.js, providing a minimal and flexi…
Passport is the most popular authentication middleware for Node.js, providing a modular framework fo…
Cookie-parser is an Express middleware that parses Cookie header values and populates req.cookies wi…
redis (node-redis) is the official Redis client for Node.js, providing a full-featured interface to …
Browse npm Packages by Category
Explore our reference of 200 popular npm packages with install commands, examples, and quick-start guides.