As the lead standardising Express 5 services, what house layout would you mandate for resource routers, validation middleware and response helpers, and what does it cost?
answer
- one composition file, fixed order
- a shared validate factory
- one home for parsed input
- thin helpers over plain res calls
- enforce with tests and lint
basics
~20 sMandate one router module per resource, a fixed app.js order, one shared validation factory that stores parsed input in one named place, and thin response helpers; the cost is a shared package every service must keep upgraded.
solid answer
~50 sI would fix the parts that cause cross-service bugs and leave the rest. Mandated: one `express.Router` module per resource with relative paths; one version router mounted in `app.js`, in a fixed order — parsers, version router, not-found middleware, error handler; one shared `validate` factory that stores parsed input on one named request property, never `req.query`, and forwards failures to the error handler; and thin helpers for create and no-content responses. Express 5 makes this simpler than it was: async rejections reach the error handler, so no wrapper is needed. The costs are real: a shared package every service must upgrade, a schema library everyone is tied to, a typed request extension to maintain, and a layout newcomers must learn. I would enforce it with a service template, a lint rule or two and HTTP tests for status codes, and revisit it if services start rebuilding a framework on top of Express.
go deeper
Know the house order of an Express app file: parsers, then the version router, then the not-found middleware, then the error handler last.
Explain why each mandated piece exists: relative router paths, one validated-input property because of the req.query getter, forwarded validation errors, and thin create and delete helpers.
Show which production bugs the convention prevents and how you would migrate existing services onto it, including removing Express 4 async wrappers and req.query assignments.
Own the trade-off: what to mandate versus leave to teams, the cost of a shared package and schema lock-in, how the layout is enforced, and when a framework would serve better than the convention.
## What the convention has to settle Express prescribes almost nothing about structure: routes, validation and responses can be wired a dozen ways, and each team that starts a service picks one. Across many services that freedom shows up as bugs that are the same bug in different files. A house layout is worth it only for the decisions that cause those bugs: 1. **Where routes live**, so anyone can find a resource's handlers. 2. **The order of the app's stack**, because Express runs layers strictly in registration order. 3. **How input is validated and where the parsed copy goes**, because handlers that read raw input are a security bug. 4. **How errors reach the response**, so every failure has one body format. 5. **How success statuses are written**, so 201, `Location` and 204 are consistent. ## A layout that works in Express 5 - **Resource routers**: one module per resource exporting an `express.Router()` with paths relative to its mount. - **One composition file**: `app.js` registers, in this order, the body parsers, the version router (`app.use('/v1', v1)`), the not-found middleware, and the error handler — and exports the app without calling `listen`, so tests and the server entry point share it. - **One validation factory**, in a shared module: `validate({ body, query, params })` parses each part, stores the result on one named property such as `req.valid`, and forwards failures with `next(err)` so the error handler owns the body. - **Thin response helpers**: `sendCreated(res, location, body)` and `sendNoContent(res)`, one per pairing, never a generic `send(res, status, body)`. - **No async wrapper**: Express 5 forwards a rejected promise from a handler or middleware to error handling, so the wrapper helpers Express 4 codebases needed are dead weight. ## Where to be strict and where to leave room | Element | Mandate? | Why | |---|---|---| | Stack order in `app.js` | yes | a wrong order is a silent bug: `undefined` bodies, HTML 404s, skipped error handlers | | Validated-input property | yes | Express 5's `req.query` getter forces a choice; one name avoids five | | Error forwarding from the validator | yes | one error body format across services | | Router file layout | mostly | findability matters more than the exact folder names | | Response helpers | for create and delete | the status and header pairings are where mistakes cluster | | Handler internals | no | business code is the team's own | ## What it costs - **A shared package** for the factory and helpers: versioned, released, and upgraded in every service, or it drifts into several copies. - **Lock-in to one schema library**, since the factory calls its parse API; switching later means touching every route. - **A typed request extension** (`req.valid`) that the TypeScript services must declare, and keep in step with the factory. - **Onboarding**: newcomers learn the house idioms on top of Express's own API, and helpers can hide what Express does underneath. - **Upgrade work** when Express itself changes; the 4-to-5 move broke exactly the patterns a convention tends to encode, such as assigning to `req.query`. ## Alternatives a lead should weigh - **No convention**, only review guidelines: cheapest up front, most drift later. - **Generating routers and validators from an API description**: one source of truth for the contract, but a code generator and its templates to own. - **An opinionated framework on top of Express**: when the house layout has grown its own module system, guards and request pipeline, a framework that ships those may cost less than maintaining a home-made one. ## How to enforce it - A **service template** that already contains `app.js` in the right order. - A **lint rule** flagging assignment to `req.query` and bare `res.status(201)` outside the helper module. - **HTTP-level tests** asserting status codes, the `Location` header and the JSON 404 for every resource. - A short **decision record** saying why each mandate exists, so it can be dropped when the reason goes away. The test of a good convention is that a new engineer can open any service, find a resource's routes in one place, read `app.js` top to bottom as the request's path, and never have to ask where validated input lives. Anything the convention mandates beyond that should earn its place by a bug it prevents.
- Would you put the validation factory in a shared package or copy it into each service?A shared package when there are more than a handful of services: one fix reaches everyone and the property name cannot drift. The price is release discipline — a versioned package, a changelog, and upgrades in every service. For two or three services a copied, documented module is cheaper; once copies diverge, that is the signal to extract it.
- When does a house layout become a reason to adopt an opinionated framework instead?When the convention keeps growing: dependency wiring, per-route guards, a request pipeline, generated docs. At that point the team maintains a private framework with no community and no docs. A framework that ships those concepts costs a migration and a learning curve, but stops the in-house layer from growing without an owner.
- Believing the shared error handler can sit anywhere in the app.js stack.
- Keeping an async wrapper around every Express 5 handler out of habit.
- Letting each service pick its own property for validated input.
- Treating the app.js registration order as a style preference.
- Calling a shared validation package free once it is written.
Assumes Express 5: forwarded async rejections remove the Express 4 async wrapper from the convention, and the req.query getter decides where parsed input lives.