80%
Express & REST APIs Middleware, routing, validation, errors, REST design, GraphQL0/25 questions known
1. Express basics and middleware
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.
2. REST API design
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
3. Validation, auth and errors
Common mistakeReturning raw error messages or stack traces to clients leaks internals.
4. Uploads, cron jobs and real-time
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.
5. REST vs GraphQL
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…
Most asked (9)
QHow does the middleware chain and next() work in Express?Express keeps an ordered list of middleware and routes.
QHow does error-handling middleware work in Express?Error middleware is a function with four arguments: (err, req, res, next).
QHow do you handle errors in async route handlers?In Express 4, a rejected promise inside an async handler is not caught: the error middleware never runs, the request hangs and you get an…
QWhat is the difference between req.params, req.query and req.body?- req.params: named parts of the path.
QWhat is REST? What makes an API RESTful?REST (Representational State Transfer) is an architectural style for APIs over HTTP.
QExplain the main HTTP methods. Which are safe and which are idempotent?- GET: read.
QWhich HTTP status codes should an API use, and when?- 200 OK success with a body; 201 Created after POST (add a Location header); 204 No Content success with no body (DELETE) - 301/308…
QHow do you paginate an API? Offset vs cursor pagination?Offset pagination: ?page=3&limit=20 → LIMIT 20 OFFSET 40.
QWhat is CORS, and how do you configure it in Express?By default, the browser's same-origin policy stops JavaScript on https://app.com from reading responses from https://api.com.