80%
DefinitionExpress is a minimal Node.js web framework.
FlowFlowRequest→helmet / cors→json parser→auth→rate limit→route handler→error handler→Response
Order matters: middleware runs in the order it is registered.
Call
next() to continue or next(err) to jump to the error handler; forgetting both hangs the request.Express 5 forwards rejected promises from async handlers automatically; in Express 4 wrap them or use try/catch.
Organise with
express.Router() per resource, plus controller/service layers.GET /users: list (with ?page=&limit=&sort=) · 200
GET /users/42: read one · 200 / 404
POST /users: create · 201 + Location header
PUT /users/42: replace · 200 / 204
400: bad request / validation failed
401: not authenticated
403: authenticated but not allowed
Common mistakeReturning raw error messages or stack traces to clients leaks internals.
File uploads: multer (multipart) with size and type limits; store in S3, keep only the key in the DB; or let the client upload…
Cron jobs:
node-cron for simple schedules; with many instances use a queue (BullMQ) or a lock so the job runs once.Real-time: WebSockets via
ws or Socket.IO; Server-Sent Events for one-way server → client updates.Webhooks (e.g.
Endpoints: many (one per resource) · one (/graphql)
Shape of data: fixed by the server · client asks for exact fields
Over/under-fetching: common · avoided
Caching: easy with HTTP caching · needs client cache (Apollo, urql)
ReviseMiddleware chain with next(), error handler last, validate every input (Zod), auth → role checks, correct status codes, idempotent methods…