Why do APIs need versioning, and what are the common ways to version a REST API? How would you structure the folders of a medium-sized Node.js API?
Show the explanation
Model answer. Versioning lets you make breaking changes (renaming a field, changing a response shape) without breaking apps that still use the old contract.
Common approaches:
- URL path:
/api/v1/students(most common; easy to see and cache). - Header:
Accept: application/vnd.example.v2+json(cleaner URLs, harder to test by hand). - Query string:
?version=2(simple, but easy to forget).
Version only for breaking changes; adding an optional field is not one. Announce deprecations and keep the old version alive for a set period.
A maintainable structure separates concerns:
src/
app.js express app: middleware, routers, error handler
server.js starts listening (keeps app testable)
config/ environment variables, validated at start-up
routes/v1/ routers: URL → controller
controllers/ read the request, call a service, shape the response
services/ business rules, no req/res
models/ or db/ database queries
middleware/ auth, validation, rate limiting
utils/
tests/Keeping HTTP details out of services makes them reusable from jobs, queues and tests.
Original UniCircle question. Last reviewed 22 Oct 2026.
