Skip to content

ergo-router

v0.1.0 Node.js ≥ 22 Pure ESM
Terminal window
npm install ergo-router ergo

ergo-router has a peer dependency on ergo.

ergo-router provides path-based request dispatch built on find-my-way with automatic REST compliance and a declarative pipeline builder.

BehaviorStandardDescription
405 + AllowRFC 9110 §15.5.6Requests to a known path with an unsupported method receive 405 with an Allow header listing valid methods
HEAD derivationRFC 9110 §9.3.2HEAD requests automatically derive from GET handlers
OPTIONSRFC 9110 §9.3.7OPTIONS requests return Allow header with supported methods
PATCH requiredCustomRoutes with PUT must also register PATCH (or explicitly opt out)
MiddlewareDescriptionStandard
Security headersHSTS, CSP, X-Content-Type-OptionsRFC 6797
CORSCross-Origin Resource SharingFetch Standard
Rate limitingGlobal rate limiter with 429 responsesRFC 6585 §4
Request IDX-Request-Id generation and propagationConvention

Declaratively compose ergo middleware for each route:

import createRouter from "ergo-router";
const router = createRouter({
defaults: {
accepts: { types: ["application/json"] },
},
});
router.post("/users", {
auth: { strategies: [bearerStrategy] },
validate: { body: userSchema },
execute: createUser,
});
import createRouter, { graceful } from "ergo-router";
const router = createRouter({ /* ... */ });
const { server, shutdown } = await graceful(router.handle(), {
port: 3000,
onShutdown: async ({ log }) => {
await disconnectDatabase();
log.info("Cleanup complete");
},
});

graceful() handles signal listeners (SIGINT, SIGTERM), connection draining with a configurable timeout, and startup/shutdown lifecycle hooks.