API Reference

express-session

Session middleware for Express with signed cookies, server-side session stores, rolling expiry, and logout invalidation.

Official Documentation ↗
CompatibilityThis documentation covers version express-session 1.18+; store packages and cookie prefixes vary by deployment. Check the official docs for the latest API surface.

Installation

npmnpm install express-session
pnpmpnpm add express-session
yarnyarn add express-session

Methods

express-session stores session data server-side and gives the browser a signed session id cookie. It is a stateful authentication primitive: logout can revoke the session immediately, unlike a stateless JWT. The default memory store must be replaced for production.

Use HttpOnly, Secure, and a deliberate SameSite value. A __Host- prefix requires Secure, Path=/, and no Domain. Configure Redis or another store with its own connection budget and availability plan. The session secret must be long, random, and rotated with a plan for existing cookies.

Session lifecycle

Keep session data small and non-sensitive. Regenerate the session id on login to prevent fixation, destroy it on logout, and invalidate other sessions on password change. Set an idle and absolute timeout; a session that lives forever is a stolen-credential risk.

Errors

The following points are quoted verbatim from the official documentation (https://expressjs.com/en/resources/middleware/session.html). Any error type, throw condition or recovery step not stated there is left unspecified; this page does not infer exception classes or return values.

Security notes

Quoted from the official documentation (https://expressjs.com/en/resources/middleware/session.html); this is not a complete threat model.

FAQ

Is the default MemoryStore safe for production?
No. It leaks memory, does not survive restarts, and is not shared across instances. Use Redis or another supported store.
Why does the session cookie not persist behind HTTPS?
Check the cookie secure, sameSite, path, and domain settings, and ensure the proxy forwards the protocol correctly. A __Host- cookie requires Secure, Path=/, and no Domain.
How do I invalidate a session on password change?
Destroy the current session and invalidate all other sessions for that user in the session store, or rotate a per-user session version and reject older versions.