docs(express): update express router example for v5 (#1681)

Added comments about how to use it with the recently release ExpressJS v5
This commit is contained in:
Ángel Cervera Claudio
2025-03-06 12:42:36 +00:00
committed by GitHub
parent c3a37cc704
commit b3361ff6ea

View File

@@ -13,7 +13,7 @@ Note that CommonJS (cjs) isn't supported. Use ECMAScript Modules (ESM) by settin
### Mount the handler
To enable Better Auth to handle requests, we need to mount the handler to an API route. Create a catch-all route to manage all requests to `/api/auth/*` (or any other path specified in your Better Auth options).
To enable Better Auth to handle requests, we need to mount the handler to an API route. Create a catch-all route to manage all requests to `/api/auth/*` in case of ExpressJS v4 or `/api/auth/*splat` in case of ExpressJS v5 (or any other path specified in your Better Auth options).
<Callout type="warn">
Dont use `express.json()` before the Better Auth handler. Use it only for other routes, or the client API will get stuck on "pending".
@@ -27,7 +27,8 @@ import { auth } from "./auth";
const app = express();
const port = 3005;
app.all("/api/auth/*", toNodeHandler(auth));
app.all("/api/auth/*", toNodeHandler(auth)); // For ExpressJS v4
// app.all("/api/auth/*splat", toNodeHandler(auth)); For ExpressJS v5
// Mount express json middleware after Better Auth handler
// or only apply it to routes that don't interact with Better Auth