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

Structuring a real backend

Understand the idea

A route that also validates, queries, and shapes the response becomes one function nobody can change safely.

The idea

This file is still one file, on purpose. The split is the functions. The is app.get. The controller is sendNotes, which only writes the . The service is listNotes. The data function is listTitles, which is the only function that knows the array. Later each function can move to its own file without changing the job. What breaks without the split is a route that validates, queries, and shapes JSON in one block. The next feature edits all three jobs at once.

What changes

Four jobs, even in one file
LayerFunction hereWhat it must not also do
Routeapp.get('/notes')Decide the business rule
ControllersendNotesKnow how the titles are stored
ServicelistNotesWrite status codes
DatalistTitlesChoose the HTTP method

The command

Titles from the data function

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

The body is titles Milk and Eggs. The route did not read the array. listTitles did.

Guided challenge

Return titles from the service

sendNotes currently puts the raw notes array in the JSON. Change it so the titles field comes from listNotes().

Return titles from the service

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
"titles":["Milk","Eggs"]

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 should a route function do?
    02Why does the split matter more as the app grows?

    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