express-session

Simple, pluggable session middleware for Express that signs session IDs into a cookie and delegates data storage to any compatible store.

Library
npm
v1.19.0
6,355 stars
MIT License

Repository Health

Pre-computed score based on development activity, maintenance, community, maturity, and trend momentum. How we score it →
68 /100 Good
Development Activity 52
Maintenance 40
Community 80
Maturity 60
Momentum 40

Technical Analysis

AI-assessed by reading the actual repository — architecture, code quality, innovation, and documentation. How we score it →
69 /100 Good
Architecture 78
Code Quality 82
Innovation 60
Learning Curve 55

express-session is the standard session middleware for Express applications, attaching req.session to every request, signing the session ID into an HttpOnly cookie, and persisting the actual session data through a pluggable Store interface rather than in the cookie itself. It ships with an in-memory MemoryStore intended only for local development, and defers real persistence to any of dozens of community-maintained stores backed by Redis, MongoDB, PostgreSQL, and other databases.

Maintained by the Express core team, it exposes fine-grained cookie controls (domain, path, expires/maxAge, sameSite, secure, partitioned, priority) including ‘auto’ resolution for secure and sameSite behind reverse proxies, plus resave/rolling/saveUninitialized flags that let applications tune exactly when sessions get written back to the store.

What You Get

  • Drop-in Express middleware - a single session() call attaches req.session, req.sessionID, and req.sessionStore to every request.
  • Pluggable Store interface - session/store.js defines a minimal get/set/destroy (plus optional touch) contract implemented by dozens of third-party stores.
  • Fine-grained cookie configuration - control domain, path, expires/maxAge, sameSite, secure, partitioned, and priority attributes, including a callback form for per-request cookie options.
  • Signed session IDs - session IDs are HMAC-signed via cookie-signature with support for secret rotation using an array of secrets.
  • Built-in MemoryStore - a bundled non-production store for local development and quick prototyping.

Common Use Cases

  • Server-rendered login sessions - track an authenticated user via req.session.user across requests without a separate auth token.
  • Per-visitor state in traditional web apps - store shopping-cart contents, view counters, or multi-step form progress server-side.
  • Reverse-proxy deployments - use cookie.secure/sameSite ‘auto’ plus Express’s trust proxy setting to correctly mark cookies as secure behind a load balancer.
  • Multi-instance deployments backed by shared stores - swap MemoryStore for connect-redis/connect-mongo/connect-pg-simple to share sessions across horizontally scaled processes.

Under The Hood

Architecture

index.js (741 lines) exports a single session() factory that closes over per-instance config (secret, store, cookie options, resave/rolling/saveUninitialized flags) and returns Express middleware; the middleware wraps res.end/res.write to guarantee the session is saved, touched, or destroyed exactly once before the response completes, and uses the on-headers package to inject Set-Cookie just before headers flush. Persistence is delegated to session/store.js: Store extends Node’s EventEmitter and provides regenerate/load/createSession helpers built on required prototype methods (get/set/destroy) that concrete stores must implement — this is the plug-point that the wider session-store ecosystem (connect-redis, connect-mongo, and dozens more) hooks into. session/session.js defines the Session constructor attached to req.session with save/reload/destroy/touch delegating back to req.sessionStore, and session/cookie.js wraps cookie serialization and expiry math in a Cookie class exposed on req.session.cookie. The layout is flat and clearly separated (entry/store/session/cookie) rather than deeply layered; the abstraction most external code depends on, and that would break the most on change, is the Store method contract.

Tech Stack

A plain Node.js CommonJS module (no TypeScript, no build step) targeting Node >= 0.8.0. Runtime dependencies are minimal and pinned with tilde ranges: cookie for Set-Cookie parsing/serialization, cookie-signature for HMAC-signed session IDs, debug for namespaced logging, depd for deprecation warnings, on-headers to hook the response header flush, parseurl for cheap URL parsing, safe-buffer for older-Node Buffer compatibility, and uid-safe for cryptographically random session ID generation. Dev tooling is Mocha plus nyc for coverage, supertest and cookie-parser for integration-style HTTP tests, and ESLint (with a markdown plugin that lints the README’s code fences) for style. CI runs via GitHub Actions alongside dedicated CodeQL and OpenSSF Scorecard workflows for supply-chain scanning. No database or ORM ships with the package — all persistence is delegated to third-party Store implementations.

Code Quality

test/session.js is a single large Mocha suite (65 describe blocks, roughly 160 individual tests) covering nearly every option combination — resave, rolling, saveUninitialized, unset, proxy, secure/sameSite ‘auto’, dynamic cookie functions, and RFC 6265 path matching — alongside a dedicated test/cookie.js for Cookie-class behavior; supertest drives real HTTP requests against Express apps assembled in test/support, with nyc producing coverage for the project’s Coveralls badge. Error handling is explicit and defensive: options are validated with thrown TypeErrors during setup (genid must be a function, unset must be destroy/keep, a secret array must be non-empty), and runtime store errors are deferred to next(err) instead of being swallowed. The code is plain ES5-style CommonJS with no static types, but every public function carries JSDoc-style @param/@return/@public/@private comments that double as documentation, and .eslintrc.yml enforces consistent style (eqeqeq, indentation, no mixed spaces/tabs, one-var).

API Design The public surface is essentially one call — app.use(session({…})) — after which req.session, req.sessionID, and req.sessionStore are simply available for the rest of the request lifecycle, with no further imports needed for the common case. Its most notable design choice is the deliberately minimal Store contract (only get/set/destroy required, touch optional), which is a stable long-lived convention rather than a technically novel mechanism, but one that has let a very large ecosystem of independent session-store packages stay compatible without coordinating with this repo. Newer additions — ‘auto’ resolution for cookie.secure/sameSite based on request TLS state, and a callback form for per-request cookie options — reduce boilerplate for reverse-proxy and multi-tenant setups. Overall this is well-worn, conventional middleware design rather than an innovative architecture.

Used by 24 apps in this directory

TypeScript
87%
MIT

medusa

Ecommerce

36,897

The most flexible open-source commerce platform — build B2C, B2B, and marketplace applications with modular, composable commerce primitives.

View details
94
Repo Health
87
Technical
63
Dependency
Built with
TypeScript 87%
JavaScript 12%
Updated 6 days ago
Clojure
57%
Other

Metabase

Analytics

49,827

The open-source BI platform that lets anyone ask questions and build dashboards without writing SQL — with an embedded analytics SDK and AI-powered query assistant included.

View details
95
Repo Health
84
Technical
62
Dependency
Built with
Clojure 57%
TypeScript 38%
Updated 5 days ago
JavaScript
94%
AGPL 3.0

MyApi

AI Agents

23

A privacy-first personal API gateway for AI agents — connect your services once, issue scoped tokens to agents like OpenClaw, Hermes, or Claude Code, and keep a full audit trail instead of scattering raw credentials across .env files.

View details
37
Repo Health
64
Technical
73
Dependency
Built with
JavaScript 94%
Updated 2 weeks ago
TypeScript
97%
Other

nango

Authentication · Automation · Developer Tools

12,963

Build product integrations with AI using 800+ APIs — auth, proxy, and TypeScript functions on production-grade infrastructure.

View details
94
Repo Health
85
Technical
67
Dependency
Built with
TypeScript 97%
Updated 6 days ago
JavaScript
88%
GPL 3.0

NodeBB

Community

15,242

Modern Node.js forum software with real-time WebSockets, multi-database support, and a plugin ecosystem — the community platform built for the open web and the Fediverse.

View details
97
Repo Health
78
Technical
70
Dependency
Built with
JavaScript 88%
Go Template 11%
Updated 5 days ago
TypeScript
26%
AGPL 3.0

Omnivore

Bookmarks Archiving · Knowledge Management · Note Taking

16,356

Self-hosted read-it-later platform with highlights, newsletters, PDFs, and seamless Obsidian and Logseq integration.

View details
90
Repo Health
74
Technical
65
Dependency
Built with
TypeScript 26%
JavaScript 24%
HTML 20%
Updated 6 days ago
JavaScript
64%
AGPL 3.0

overleaf

Collaboration · Productivity

18,266

Open-source, real-time collaborative LaTeX editor with sandboxed compilation and full TeXLive support for self-hosted academic and research teams.

View details
84
Repo Health
80
Technical
59
Dependency
Built with
JavaScript 64%
TypeScript 30%
Updated 1 months ago
TypeScript
90%
MIT

Plasmic

CMS · Design Tools · Low Code Platforms

7,125

The open-source visual builder that lets teams design React apps and websites with drag-and-drop while integrating seamlessly with your codebase.

View details
81
Repo Health
78
Technical
64
Dependency
Built with
TypeScript 90%
Updated 6 days ago
TypeScript
96%
Other

Refly

AI Development · Automation · No Code Platforms

7,537

Build deterministic, versioned agent skills from natural language—run them anywhere from Claude Code to Slack.

View details
68
Repo Health
76
Technical
63
Dependency
Built with
TypeScript 96%
Updated 1 weeks ago

Join founders buildingwith open source

Opinionated takes, migration guides, cost-saving tips, and insights from the open source ecosystem.

Subscribe on Substack
Join 750+ subscribers