Resource-oriented API design
Understand the idea
Name the resource, then pick the method that matches the change you mean.
The idea
An should name a thing: /notes, /notes/7. The method is the promise. GET reads. POST adds a new resource. PATCH changes the fields you send and leaves the rest. PUT replaces the stored record with what you sent. DELETE removes it. means repeating the same write does not create a second effect. PUT of the same id stays one record. POST of the same title creates another.
What changes
| Method | Promise | Calling it twice |
|---|---|---|
| GET | Read a resource | Still a read |
| POST | Create a new resource | A second resource |
| PUT | Replace the stored record | Still one record |
| PATCH | Change the fields you named | Those fields, not a new row |
| DELETE | Remove the resource | Still gone |
The command
PATCH keeps done
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.
PUT replaces the record
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.
POST twice, PUT twice
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
PATCH returns id 7, title Eggs, and done still false. PUT returns id 7 and title Eggs, with no done field. The output run prints post 1, post 2, put 1, put 1.
Guided challenge
PATCH only the title field
The PATCH handler currently sets done to true when a title arrives. Change it so it copies req.body.title onto note.title and leaves done alone.
PATCH only the title field
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 "title":"Eggs" "done":false
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.