{"success":true,"name":"express-middleware-runs-in-registration-order-and-error-hand","description":"Using Express and middleware mysteriously doesn't apply, or errors skip your handler","category":"ai-brimind","path":"ai-brimind/express-middleware-runs-in-registration-order-and-error-hand/SKILL.md","file":"SKILL.md","content":"---\nname: express-middleware-runs-in-registration-order-and-error-hand\ndescription: \"Using Express and middleware mysteriously doesn't apply, or errors skip your handler\"\n---\n\n# Express Middleware Runs in Registration Order — and Error Handlers Need Exactly Four Parameters\n\n## When to use\nProject uses Express, and body parsing, auth, or CORS behaves as if absent for some routes, or a custom error handler never fires.\n\n## Example request\nroutes registered before the body parser → req.body is undefined for them; order decided, not chance. auth middleware added after the routes it should guard → those routes ship unguarded; a security hole made of line order. error handler written with three parameters → Express doesn't recognize it as one, errors fall through to the default page; the four-argument signature rule. catch-all route placed first → it swallows every request; greedy early match. — each treated registration order as decoration.\n\n## Instructions\nAct as a factory planner who knows the assembly line processes every unit in station order — a station placed after packaging never touches the product. Express is exactly that line: each request flows through middleware and routes in the order they were registered, top to bottom, and placement IS behavior. Get the canonical order right: global infrastructure first (body parsers, CORS, logging), then authentication, then the routes — because a route registered above the body parser receives requests whose bodies were never parsed, and a route above the auth middleware is simply unprotected; both fail silently, per the loud-failure rules the worst kind. Specificity flows downward too: specific routes before parameterized catch-alls, or the greedy pattern eats its siblings. Then the error handler's peculiar contract: Express identifies error middleware ONLY by its parameter count — exactly four (err, req, res, next) — so a three-parameter 'error handler' is invisible to the mechanism and errors sail past it to the default handler; register it LAST, after all routes, and remember async route errors reach it only if passed to next (or via the framework's async support), per the async-catching rules. When middleware 'doesn't work', don't debug its internals first — read the registration order in the app's entry file top to bottom, walking the line the request actually travels. Only proceed once the order tells the correct story: parse, protect, route, and catch errors at the very end with four parameters.\n\n## Metadata\n- role: developer\n- level: tactical\n- places: vibeCoding\n- origin: ai.brimind.pro skill #825\n","linked_files":[],"origin":"ai.brimind.pro","owner_id":null,"source":"router"}