Skip to content

The Express package

For: a developer of an Express 5 app on Node 22.18 or newer.

What it does: this page lists the exports of the package, the steps of the mount, and the paths that the protect rule gets. The export check holds this page to packages/express/src/index.ts.

  1. Mount core before the body parser of the app: app.use("/auth", authHandler(auth)), then app.use(express.json()).
  2. Read the session: app.use(authSession({ auth })), then getSession(res) in a route.
  3. Protect paths: authSession({ auth, protect: (p) => p.startsWith("/account"), signInPath: "/sign-in" }).
Export What it does
authHandler Answers the handler of the routes of core.
authSession Answers the middleware that sets res.locals.session, sends a person with no session on a protected path to the sign-in page, and adds the renewed cookie.
getSession Answers the session that the middleware set, or null.
toWebRequest Answers the web request of an Express request: the full URL, every header, and the body.
sendWebResponse Writes a web response to Express, with each Set-Cookie line.
AuthSource The auth instance, or a function that makes it at the first request.
ExpressHandlerOptions The options of authHandler: waitUntil, the hook that keeps work alive after the answer. It has no default.
ExpressSessionOptions The options of authSession: auth, protect, signInPath, and basePath.
  • protect gets the text that the router of Express matched: the raw request path, with its case.
  • The path is not decoded, so %61 stays %61. The binding does not resolve “.” and “..”.
  • Express matches without case by default, on the app and on each express.Router(). It also matches the hex digits of an escape without case.
  • So the binding also runs the rule on the lower-case form of the path, and on that form with each escape in upper-case hex.
  • Express matches without case by default. Write the rule in lower case: p.startsWith("/myAccount") misses /myaccount, which runs the same page.
  • Express routes with strict routing off by default, so /account also matches /account/. The binding also runs the rule on each form with its trailing slashes removed.
  • A path needs a session when the rule holds for any form. Also set app.set("case sensitive routing", true), so /ACCOUNT gets a 404.
  • An exact rule covers one route only. It does not cover a route with a parameter, such as /account{/:tab}.
  • For example, /account/%2F runs that route with tab “/”. Use a prefix rule: p === "/account" || p.startsWith("/account/").
  • Write a rule for a non-ASCII path in the escaped form, such as p.startsWith("/caf%C3%A9"). Node answers 400 to a raw UTF-8 request line before Express runs.
  • Node keeps a detached promise alive, so a plain server needs no waitUntil. On a host that stops the function after the answer, pass waitUntil in the options of authHandler. The SMS of a phone code and the flush of the events run under it (ruling 29 of plan 12i).
  • The package takes express 5.2.1 or a later 5 release as a peer dependency.
  • The Express adapter of NestJS 12 pins Express 5.2.1, so the binding runs there too.
  • A body parser such as express.json() may run before the routes of core. The binding writes the parsed body again in its content type.
  • Mount the routes of core before the body parser of the app. Core then reads each body itself, at the cap of its route, and answers 413 body_too_large past it. The parser of the app keeps its default limit for the other routes.
  • A parser that must run first answers its own 413 before core past its limit. The default limit of express.json() is 100 kB, and an SSO connection write can need more.
  • Make such a parser skip the base path of core, so core reads those bodies at their caps: app.use((req, res, next) => (req.path.startsWith("/auth/") ? next() : json(req, res, next))), with const json = express.json().
  • These are the two setups. Do not give a parser the bodies of core’s routes, not even on the two SSO connection paths only.
  • A parser reads a body before core checks the session, and it has no depth or item limit.
  • On 2026-10-30, a parser ran on the two SSO connection paths only, with their cap of 8 MiB as its limit. One body of nested arrays at that limit, with no sign-in, stopped a process with a heap of 256 MB.
  • Never raise the limit of a parser for every route: the same body then stops the process at any route.
  • The middleware reads no session for a route of core. Core reads and renews the session itself.
  • The middleware skips the session read only when the raw path and the matched path are both under the base path. Else it judges the request as a page.
  • After the sliding refresh, the answer carries the renewed session cookie.
  • The origin of the redirect comes from the protocol and the Host header. A request line in absolute form gives its path only, never the origin.
  • A Host header that does not parse gets no 500. The binding uses the origin of auth.baseURL, else “http://localhost”.
  • Behind a TLS proxy, set app.set("trust proxy", ...), so the sign-in redirect keeps https.
  • toWebRequest and sendWebResponse are the two seams to Express.
  • This is the one binding package that imports a Node module: node:stream.