Skip to content
engineering workbench.by StudyPort Academy
Learning path
Advanced18 min

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, decision, and status
BodytitleFromStatus
title MilkMilk201
title as spacesnull400
title as a numbernull400
no titlenull400

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.

    01What does express.json guarantee?
    02When should the title check run?

    Ready to call this one understood?

    Mark this lesson complete to earn 200 XP. You can always revisit it.

    Sign in to save progress
    Next lesson