express-session is a robust and widely-used session middleware for Express.js applications, currently stable at version 1.19.0. It provides server-side session storage, managing session IDs via cookies while keeping the actual session data on the server, which is a key security differentiator compared to client-side cookie storage. While the core package offers a default `MemoryStore` for development and debugging, it explicitly warns against its use in production due to memory leak risks and lack of scalability, promoting a rich ecosystem of compatible external session stores. The project maintains a steady release cadence, with recent updates focusing on features like dynamic cookie options, improved security tooling, and dependency updates, ensuring ongoing compatibility and enhancements for Node.js environments (supporting Node.js >= 0.8.0). It has evolved to directly manage session cookies, making the `cookie-parser` middleware optional and recommending careful use if both are present to avoid secret mismatches.
npm install express-sessionVerified import paths — ran on the pinned version, not inferred.
This example demonstrates how to set up `express-session` with basic configuration for a view counter. It highlights essential cookie options like `secure`, `httpOnly`, and `maxAge`, and includes a critical warning about not using the default `MemoryStore` in production, emphasizing the use of environment variables for secrets.
Implement a production-ready session store, such as `connect-redis`, `connect-mongo`, `memorystore`, or other compatible solutions listed in the `express-session` documentation.
Generally, remove `cookie-parser` if `express-session` is handling session cookies. If `cookie-parser` is required for other (non-session) cookies, ensure that both middlewares use the *same* `secret` key.
Consistently use `maxAge` (in milliseconds) for controlling cookie expiration, and avoid setting `expires` directly.
Explicitly set `resave` to `false` unless your session store specifically requires it. Most modern stores handle session modifications efficiently without needing `resave: true`.
Ensure that any dynamically generated or configured `expires` dates are valid `Date` objects. It is best practice to use `maxAge` instead of `expires` for session cookies.
Always keep `cookie.httpOnly` set to `true` (the default) unless there is a very specific, well-understood requirement for client-side JavaScript to access the session cookie.
Provide a strong, unpredictable string or an array of strings as the `secret` value in the `session()` options. It should be stored in an environment variable for security.
Ensure you are calling the imported `session` module to create the middleware: `app.use(session({ ... }))`. For CommonJS: `const session = require('express-session');`. For ESM: `import session from 'express-session';`.Replace the default `MemoryStore` with a production-grade, persistent session store like `connect-redis`, `connect-mongo`, or `memorystore`.
Remove `cookie-parser` if `express-session` is sufficient for your cookie needs. If `cookie-parser` is still needed for other reasons, ensure both `express-session` and `cookie-parser` use the exact same `secret` value.
Inspect the session store for malformed data. If using a custom store, verify its `get` and `set` methods. If using an existing store, check for recent updates to `express-session` or the store implementation that might address data handling bugs.