Node.js · 17/19 6 min
REST API conventions tests expect
Resource URLs, status codes, response shapes, pagination metadata and idempotent methods.
Build labs and debugging rounds check conventions, not just data. These are the defaults most tests assume.
Routes
| Action | Route | Success |
|---|---|---|
| list | GET /notes |
200 + array (or { items, total, page }) |
| read | GET /notes/:id |
200, or 404 |
| create | POST /notes |
201 + the created object (with id) |
| replace | PUT /notes/:id |
200 + updated object |
| update fields | PATCH /notes/:id |
200 + updated object |
| delete | DELETE /notes/:id |
204 no body (or 200 + deleted object, if the spec says so) |
Errors have one shape
{ "error": "title is required" }
Pick a shape and use it everywhere. Tests often assert res.body.error exists or matches a message.
Pagination
{ "items": [...], "page": 2, "limit": 20, "total": 57, "totalPages": 3 }
pagestarts at 1;skip = (page - 1) * limit.totalPages = Math.ceil(total / limit); withtotal = 0that's 0, not 1.- Sort before paginating, with a tiebreaker (
createdAt, then_id), or items repeat or vanish between pages. - Clamp
limitso?limit=1000000can't dump the table.
Idempotency
GET, PUT and DELETE should be safe to retry: deleting twice gives 404 (or 204) the second time, not a crash. POST isn't idempotent; use an idempotency key when retries matter (payments).
Don't leak
Strip internal fields (password, __v, internal flags) from every response, including create and update, not only reads.
Practise it now
Next in Node.js
Web security basics for APIs