More in Backend Dev — page 6
API versioning: URL vs header strategies
WHAT IT TESTS: managing breaking changes. OUTLINE: version via URL path (/v1/), a custom or Accept header, or a query param; URL is visible and cache-friendly, headers keep URLs clean but are less discoverable.
Centralized error handling in an Express API
WHAT IT TESTS: designing one error path. OUTLINE: a final four-arg error middleware, an asyncHandler wrapper to funnel promise rejections via next, a custom error class with statusCode, returning uniform JSON.
404 vs 500: missing resource vs server failure
WHAT IT TESTS: 4xx versus 5xx semantics. OUTLINE: a missing resource returns 404 Not Found (client asked for something absent); a database failure returns 500 Internal Server Error (server-side fault).
Idempotency: PUT vs POST in REST
WHAT IT TESTS: understanding idempotency. OUTLINE: idempotent means repeated identical requests leave the same server state; PUT is idempotent, POST is not. Use PUT to overwrite a resource at a known URL.
Modularize routes with express.Router
WHAT IT TESTS: structuring a growing app. OUTLINE: create a Router per resource in its own file, define routes on it, export it, and mount with app.use('/products', router).
Status codes for successful POST and GET
WHAT IT TESTS: correct 2xx semantics. OUTLINE: 201 Created for a successful POST (ideally with a Location header), 200 OK for a successful GET returning data. RED FLAG: returning 200 for every success or 204 when a body is sent.
req.params vs req.query vs req.body in Express
WHAT IT TESTS: where request data lives. OUTLINE: req.params holds named route segments, req.query holds the URL query string, req.body holds the parsed payload. RED FLAG: confusing query with params, or expecting req.body without a body parser.
Design an Express route to create a user
WHAT IT TESTS: REST conventions for creation. OUTLINE: POST to a collection URL like /users, read the payload from req.body via express.json(), return 201 with the created resource. RED FLAG: using GET to create or putting data in the URL.
Conditionally apply middleware by request property
WHAT IT TESTS: knowing helpers and the type option exist. OUTLINE: use express.text({ type: 'application/xml' }) or a guard wrapper that checks req.is() then calls the parser or next().
Write a JWT authentication middleware
WHAT IT TESTS: extracting a Bearer token, verifying it, and gating access. OUTLINE: read the header, strip Bearer, jwt.verify with the secret, set req.user and next(), else send 401. RED FLAG: calling next() after sending 401, or trusting an unverified token.
Middleware execution order and sharing data via req
WHAT IT TESTS: the sequential pipeline and shared req object. OUTLINE: middleware runs top-down in registration order; each calls next(); attach data like req.user that later handlers read. RED FLAG: assuming parallel execution or using globals to pass state.
Express error-handling middleware signature
WHAT IT TESTS: recognizing the four-argument signature. OUTLINE: (err, req, res, next) with err first, identified by arity, placed last after all routes. RED FLAG: omitting the err parameter or registering it before the routes it should catch.
Application-level vs router-level middleware
WHAT IT TESTS: scoping of middleware. OUTLINE: app.use() binds to the app and runs everywhere; router.use() binds to a Router and runs only for that router's routes. RED FLAG: claiming they are interchangeable or that scope does not matter.
Write a request-logging middleware in Express
WHAT IT TESTS: the (req, res, next) signature and calling next(). OUTLINE: read req.method, log with a timestamp, then call next() to continue. RED FLAG: forgetting next() so the request hangs, or ending the response prematurely.
Parse JSON and URL-encoded bodies in Express
WHAT IT TESTS: knowledge of built-in body parsers. OUTLINE: express.json() for JSON, express.urlencoded() for form data, both registered via app.use(). RED FLAG: reaching for the deprecated body-parser package or forgetting extended option.
What is Express middleware
WHAT IT TESTS: the core Express request pipeline. OUTLINE: middleware are functions in a chain with req, res, next, they inspect or modify request and response, then call next to continue or send a response to end.
Centralized error-handling middleware
WHAT IT TESTS: Express error flow. OUTLINE: error middleware takes four args err, req, res, next, is defined last, and runs when next(err) is called or sync errors throw. RED FLAG: omitting the err parameter so Express treats it as normal middleware.
Custom API key auth middleware
WHAT IT TESTS: writing middleware with the req, res, next contract. OUTLINE: read the header from req, on missing or invalid send res.status(401) and return, on valid call next, mount before protected routes.
Modularizing routes with express.Router
WHAT IT TESTS: structuring a growing app. OUTLINE: create a Router instance in users.js, attach routes to it, export it, then mount it under a base path with app.use in the main file. RED FLAG: duplicating the base path on every route instead of mounting.
Express route order and matching
WHAT IT TESTS: top-down route matching. OUTLINE: Express checks routes in definition order, /items/new matches the literal route first, if /:id came first new would be captured as an id. RED FLAG: thinking Express picks the most specific route automatically.