Never trust client input
Understand the idea
express.json parses an object. It does not decide that the object is a note you should store.
The idea
This phase is input, abuse, and secrets. It does not issue logins or roles. The default mindset is that the body is data, not a decision. express.json builds an object. titleFrom is the contract: a string, trimmed, non-empty. Anything else is 400 and no insert. Put that function on the or at the top of the handler, before a service stores the document. A later lesson uses the same refusal habit on a model body, which can look finished and still be the wrong shape.
What changes
| Body | titleFrom | Status |
|---|---|---|
| title Milk | Milk | 201 |
| title as spaces | null | 400 |
| title as a number | null | 400 |
| no title | null | 400 |
The command
Accept a title
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.
Reject spaces
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.
Reject a number
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
Milk is 201. Spaces are 400. A number is 400. None of the failures include a stored id, because nothing was written.
Guided challenge
Reject a blank title before creating
The route always returns 201 with req.body.title. Put the titleFrom check back so a body of spaces returns 400 and does not create.
Reject a blank title before creating
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 400 title required
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.