{"success":true,"name":"order-express-middleware-deliberately-position-is-behavior","description":"Adding or moving middleware/routes in an Express (Node.js) app.","category":"ai-brimind","path":"ai-brimind/order-express-middleware-deliberately-position-is-behavior/SKILL.md","file":"SKILL.md","content":"---\nname: order-express-middleware-deliberately-position-is-behavior\ndescription: \"Adding or moving middleware/routes in an Express (Node.js) app.\"\n---\n\n# Order Express Middleware Deliberately — Position Is Behavior\n\n## When to use\nProject is Node.js with Express, and you are adding middleware, an error handler, or wondering why a route or handler never runs.\n\n## Example request\nRoute defined after app.listen or after a catch-all → Express matches top-down; order decides who runs — this is middleware chaining. express.json() placed after routes → req.body is undefined in them; parsers come before consumers. Error handler with 3 args instead of (err, req, res, next) → Express identifies error middleware by its 4-argument signature — and it must be registered LAST. Auth middleware after the routes it protects → routes above it are open — position is the security boundary.\n\n## Instructions\nAct as an Express developer who reads the middleware stack as a top-to-bottom pipeline before changing it. Before adding or moving anything, list the current order and confirm the invariants: body parsers and cookie/session middleware before any route that reads them; auth middleware before every route it must protect; specific routes before catch-alls and 404 handlers; the error handler — with the exact (err, req, res, next) 4-argument signature — registered last. When a handler 'mysteriously' never runs or req.body is undefined, the FIRST hypothesis is ordering or a missing next() call, not a broken handler — check the stack before rewriting code. Remember each middleware must either respond or call next(); doing neither hangs the request, doing both causes 'headers already sent'. Only apply the change once you have confirmed where it sits in the chain and why that position gives the intended behavior for every route above and below it.\n\n## Metadata\n- role: developer\n- level: tactical\n- places: vibeCoding\n- origin: ai.brimind.pro skill #987\n","linked_files":[],"origin":"ai.brimind.pro","owner_id":null,"source":"router"}