Headers, status codes, and pagination
Understand the idea
The status is part of the answer. The list endpoints also have to say which slice you returned.
The idea
The status is the practiced part of this lesson. 201 means created. 204 means done and there is no body. 404 means that path has no resource. 409 means the write clashes with what is already stored. is the slice: limit and offset in the query string, plus total in the body. Filtering and sorting are more query fields, not extra routes. This runner shows status and body. The create example also sets Location and echoes the request Content-Type so you can see the header value in the JSON.
What changes
| Status | When | Body |
|---|---|---|
| 201 | A resource was created | The new resource, and a Location header |
| 204 | The write worked and there is nothing to return | Empty |
| 400 | The client sent a body you will not accept | What was wrong |
| 404 | No resource at that path | A short error |
| 409 | The write clashes with current state | Why it clashed |
The command
201 Created
3s timeout · one stateless run. Node and Express use Piston when that instance accepts the job (public EMKC execute is whitelist-only; set PISTON_URL or PISTON_TOKEN). Otherwise a local Node process. MongoDB uses a fresh in-memory server per run, then it is stopped.
404 missing
3s timeout · one stateless run. Node and Express use Piston when that instance accepts the job (public EMKC execute is whitelist-only; set PISTON_URL or PISTON_TOKEN). Otherwise a local Node process. MongoDB uses a fresh in-memory server per run, then it is stopped.
204 delete
3s timeout · one stateless run. Node and Express use Piston when that instance accepts the job (public EMKC execute is whitelist-only; set PISTON_URL or PISTON_TOKEN). Otherwise a local Node process. MongoDB uses a fresh in-memory server per run, then it is stopped.
409 conflict
3s timeout · one stateless run. Node and Express use Piston when that instance accepts the job (public EMKC execute is whitelist-only; set PISTON_URL or PISTON_TOKEN). Otherwise a local Node process. MongoDB uses a fresh in-memory server per run, then it is stopped.
Page through notes
3s timeout · one stateless run. Node and Express use Piston when that instance accepts the job (public EMKC execute is whitelist-only; set PISTON_URL or PISTON_TOKEN). Otherwise a local Node process. MongoDB uses a fresh in-memory server per run, then it is stopped.
Filter and sort
3s timeout · one stateless run. Node and Express use Piston when that instance accepts the job (public EMKC execute is whitelist-only; set PISTON_URL or PISTON_TOKEN). Otherwise a local Node process. MongoDB uses a fresh in-memory server per run, then it is stopped.
The mistake
Check the result
Create is 201, location /notes/8, contentType application/json. The missing id is 404. Delete is 204 with an empty body. The taken title is 409. Offset 2 returns Bread and total 3. The filtered sort returns Bread and Eggs, total 2.
Guided challenge
Honor limit from the query string
The page handler ignores the query and forces limit 10. Read limit from req.query.limit with a default of 2 so GET /notes?limit=2&offset=2 returns the third item.
Honor limit from the query string
3s timeout · one stateless run. Node and Express use Piston when that instance accepts the job (public EMKC execute is whitelist-only; set PISTON_URL or PISTON_TOKEN). Otherwise a local Node process. MongoDB uses a fresh in-memory server per run, then it is stopped.
EXPECTED RESULT
status 200 "items":["Bread"] "total":3
Hints 0/2
Use all hints or make 3 failed checks (0/3).
Free exploration
Change this example and run it again. Each run is a new process. Nothing is saved.
Your variation
3s timeout · one stateless run. Node and Express use Piston when that instance accepts the job (public EMKC execute is whitelist-only; set PISTON_URL or PISTON_TOKEN). Otherwise a local Node process. MongoDB uses a fresh in-memory server per run, then it is stopped.
Quiz
A quick check for understanding. Retry as often as you like; your learning path stays open.
Ready to call this one understood?
Mark this lesson complete to earn 200 XP. You can always revisit it.