Skip to content

Getting Started

  • Node.js 22 or later (pure ESM)
  • npm (ships with Node.js)
Terminal window
npm install ergo ergo-router

Create a server with a single route that demonstrates all four Fast Fail stages:

import createRouter, { graceful } from "ergo-router";
const router = createRouter({
defaults: {
accepts: { types: ["application/json"] },
},
});
router.get("/users/:id", {
auth: {
strategies: [
{
type: "Bearer",
authorizer: async (attributes, token) => {
if (!token) return { authorized: false, info: { statusCode: 401 } };
return { authorized: true, info: { userId: token } };
},
},
],
},
validate: {
params: {
type: "object",
properties: { id: { type: "string", minLength: 1 } },
required: ["id"],
},
},
execute: (req, res, acc) => ({
response: {
body: {
id: acc.route.params.id,
name: "Jane Doe",
requestedBy: acc.auth.userId,
},
},
}),
});
await graceful(router.handle(), { port: 3000 });
RequestResult
GET /users/42 with Accept: text/xml406 Not Acceptable — negotiation fails
GET /users/42 without Authorization401 Unauthorized — authorization fails
GET /users/42 with valid Bearer token200 OK — all stages pass

Every error response is a RFC 9457 Problem Details JSON object.