Designing REST resources and routes
Build a Node HTTP API from scratch, model data in SQL and MongoDB, and reason about transactions, indexes, caching and authentication so the endpoint you ship stays correct under load.
What you will be able to do
#- Name resources with nouns and model collections versus single items.
- Map create, read, update and delete onto methods and URLs.
- Decide when to nest a resource and when to promote it to the top level.
Overview
#REST models your domain as resources addressed by URLs. The URL names the thing; the method says what to do to it. Keep URLs stable and predictable.
Naming rules
- Use plural nouns for collections:
/articles,/users. - Address one item by id:
/articles/42. - Nest only when the child cannot exist without the parent:
/articles/42/comments. - Do not put verbs in the path:
POST /articles/42/publishis an action, so prefer a sub-resource like/articles/42/publicationswhen the action creates something, or accept a small action endpoint when it does not.
Method mapping
GET /articles list
POST /articles create
GET /articles/42 read one
PUT /articles/42 replace
PATCH /articles/42 partial update
DELETE /articles/42 remove
Filtering, sorting, pagination
Query parameters shape a collection without changing the resource: /articles?status=published&sort=-createdAt&limit=20. Keep pagination explicit and stable; see the pagination strategies reference.
Common pitfalls
- Deep nesting that forces long URLs and ambiguous ownership.
- Using
GET /getArticlesinstead ofGET /articles. - Returning a bare array with no pagination metadata, which becomes unusable at scale.
Exercises
#Reference answers and expected output are authored, not executed in this browser. Nothing you type is run, and there is no Run or Pass control on this page.
Exercise 1Implement
Write the method and URL to change only the title of article 42, and to list comments for that article.
starter.http// 1) partial update of one field // 2) list comments belonging to the articleReveal the reference answer
reference.httpPATCH /articles/42 with body { "title": "..." } GET /articles/42/commentsAuthored expected output: PATCH /articles/42 and GET /articles/42/comments
A partial update uses PATCH on the item. Comments belong to the article, so nesting under the article id is correct.
Exercise 2Explain
Explain why returning every article in one response is a design flaw and what you return instead.
starter.textGET /articles -> [ ... 200000 items ... ]Reveal the reference answer
reference.textAn unbounded response grows with the data, so it can exhaust memory, saturate bandwidth and time out the client. Return a page of items plus metadata such as the next cursor or total count, and let the client request the next page.Authored expected output: An explanation naming unbounded payload size and a paginated response with cursor or offset metadata.
Pagination bounds the response size and keeps latency predictable as the dataset grows.
Common misconceptions
#- REST requires a verb in the URL: the method carries the verb and the URL names the resource.
- Nested URLs are always better: deep nesting couples unrelated resources and complicates authorization.
Follow-up practice
#- backend-databases-json-validation
Your progress
#Answers, drafts and completion marks are stored only in this browser. Nothing is sent to a server and no account is used.
Verification: authored. Answers and outputs on this page are authored reference material, not executed results.