Crux Corner is a small community bouldering wall in a converted school gym. Ines sets its routes, and an Express API keeps track of every boulder problem she sets. The API works. However, it trusts whatever it’s sent, and Ines keeps finding nonsense in its data.
In this course, you’ll make that API check every request before anything reaches the data and answer every failure with a clear error and the right status code. In this lesson, you’ll get the API running and send it the nonsense yourself, so you can see exactly what gets through and why. Start by setting up the wall’s API on your own computer.
Get the Wall’s API Running
Until now, you’ve mostly been on the client side of an API. You’ve sent requests with curl and fetch(), read the status codes that came back, and handled the failures. In this course, you decide what the server refuses and what it says when it does. A server that stores whatever arrives hands its mistakes to every client that reads the data later.
The course assumes you can write Express 5 routes that read JSON bodies, route parameters, and query strings, and that you’ve used Zod’s .safeParse() and z.infer in TypeScript. You don’t need any particular earlier project, because the course gives you a starting project to download.
You’ll work through it in three steps. First, you’ll learn middleware, Express’s way of running a function for many requests before they reach a route. Then you’ll use middleware to check every request’s body, route parameters, and query values with Zod schemas. Finally, you’ll send every error from one place, in one consistent shape.
You type each lesson’s code into the starting project and call the API with curl -i after every change, so you always see the real exchange. If your code and the lesson’s disagree, compare yours with the finished-code folder in the same download. After each section, a coding exercise in your browser has you guard the wall’s shoe rental shelf. The course ends with a project where you harden the members’ climbing log.
You need Node.js installed (a current version; the examples use Node 26). Unzip the course’s code download, open a terminal in its starting-project folder, and install the project’s packages with npm:
$ npm install
added 79 packages, and audited 80 packages in 2s
32 packages are looking for funding
run `npm fund` for details
found 0 vulnerabilities
The numbers and the time in your output may differ. The packages include Express, its type definitions, TypeScript, and Zod, which you’ll need when you add schema validation. Now start the API with the project’s dev script:
$ npm run dev
> [email protected] dev
> node --watch server.ts
Crux Corner API on http://localhost:3000
The dev script runs node --watch server.ts, so Node.js restarts the API whenever you save server.ts or one of the modules it loads. Leave this terminal running, and open a second terminal in the same folder for your requests.
Before you send the API anything, look at what’s in the project. Here are the files you’ll work with most:
problems.json: Ines’s boulder problems, the API’s dataserver.ts: Starts the app on port 3000app.ts: Builds the app, addsexpress.json(), and mounts the routersroutes/problems.ts: The/problemsroutes to list, find, add, and retire a problemresponses.ts: TheerrorBody()andreadJsonBody()helpersstore.ts: Loads a JSON file once and saves every change safely
There’s also log.json, the members’ climbing log, and its router. You’ll use them in the course’s project. Each problem in problems.json has a string id, a name, a hold colour, a grade from "V0" to "V8", the setOn date when Ines set it, and a retired flag:
problems.json
[
{ "id": "1", "name": "Chalk Dust", "colour": "yellow",
"grade": "V0", "setOn": "2026-09-05", "retired": false },
{ "id": "2", "name": "The Slab", "colour": "blue",
"grade": "V2", "setOn": "2026-09-05", "retired": false },
{ "id": "3", "name": "Crimp City", "colour": "green",
"grade": "V4", "setOn": "2026-10-10", "retired": false },
{ "id": "4", "name": "Gym Bars", "colour": "red",
"grade": "V5", "setOn": "2026-10-10", "retired": true },
{ "id": "5", "name": "Roof Dance", "colour": "red",
"grade": "V6", "setOn": "2026-11-07", "retired": false },
{ "id": "6", "name": "Headmaster", "colour": "black",
"grade": "V8", "setOn": "2026-11-07", "retired": false }
]
Grades on a bouldering wall run from V0, the easiest, upward. Nothing at Crux Corner is harder than V8, though the API is meant to accept grades up to V10 for when Ines sets something tougher.
In this lesson, you’ll work with the route that adds a problem. Open routes/problems.ts and find the POST route:
routes/problems.ts
problemsRouter.post("/", async (req, res) => {
const body = readJsonBody(req, res);
if (body === undefined) {
return;
}
const fields = toNewProblem(body);
if (fields === undefined) {
const message =
"Send a name, a colour, a grade and a setOn date as strings.";
res.status(422).json(errorBody("invalid_problem", message));
return;
}
const problem = await store.create({ ...fields, retired: false });
res.status(201).location(`/problems/${problem.id}`).json(problem);
});
The route already checks its input by hand. First, readJsonBody() answers 415 when the body isn’t sent as JSON and 400 when there’s no body. It returns undefined once it has answered, which is why the route stops there.
Next, toNewProblem() in problems.ts checks that the parsed body is an object with name, colour, grade, and setOn present, each as a string. If one is missing or isn’t a string, the route answers 422 with the API’s error shape, { error: { code, message, details } }. Otherwise, it saves the problem with a new ID and answers 201 with a Location header.
You can see the checks work. In your second terminal, send a problem with no name:
$ curl -i -H "Content-Type: application/json" \
-d '{"colour":"yellow","grade":"V1","setOn":"2026-11-26"}' \
http://localhost:3000/problems
HTTP/1.1 422 Unprocessable Entity
X-Powered-By: Express
Content-Type: application/json; charset=utf-8
Content-Length: 123
ETag: W/"7b-u/qCdFKjgeaOFc2eUKKFgtVSjaY"
Date: Fri, 27 Nov 2026 10:19:15 GMT
Connection: keep-alive
Keep-Alive: timeout=5
{"error":{"code":"invalid_problem",
"message":"Send a name, a colour, a grade and a setOn date as strings.",
"details":null}}
The JSON is wrapped here for display. The API refused the problem and saved nothing. Its message says what it expects, though not which field was wrong.
Send Ines’s Nonsense
The API has a second line of defense, or so it seems. Ines doesn’t type curl commands. She adds new problems from a small page that Arjun, one of the members, built for the front desk’s laptop. Its grade dropdown offers only V0 to V8, its date picker stops at today, and it won’t submit a name that’s only spaces. Between the page and the route’s checks, the wall’s data looks safe.
A page is only one client, though, and the API answers any request from anyone. Test that with three requests the page would never send: a V14, a problem set next year, and a name made of three spaces. The second one, Early Bird, is dated 2027-06-15. If you’re reading this after June 2027, then put any date after today in its place.
Before you send them, predict which ones the API refuses and with what status:
$ curl -i -H "Content-Type: application/json" \
-d '{"name":"Sky Ladder","colour":"black",
"grade":"V14","setOn":"2026-11-26"}' \
http://localhost:3000/problems
$ curl -i -H "Content-Type: application/json" \
-d '{"name":"Early Bird","colour":"green",
"grade":"V3","setOn":"2027-06-15"}' \
http://localhost:3000/problems
$ curl -i -H "Content-Type: application/json" \
-d '{"name":" ","colour":"yellow",
"grade":"V1","setOn":"2026-11-26"}' \
http://localhost:3000/problems
The API refuses none of them. All three get 201 Created. Here’s the first exchange:
$ curl -i -H "Content-Type: application/json" \
-d '{"name":"Sky Ladder","colour":"black",
"grade":"V14","setOn":"2026-11-26"}' \
http://localhost:3000/problems
HTTP/1.1 201 Created
X-Powered-By: Express
Location: /problems/a4c52b50-655c-45a5-8825-2c1ee18c6a5a
Content-Type: application/json; charset=utf-8
Content-Length: 133
ETag: W/"85-qwpezKVVsFKWKH+x7mdJhKSdOGQ"
Date: Fri, 27 Nov 2026 10:19:15 GMT
Connection: keep-alive
Keep-Alive: timeout=5
{"id":"a4c52b50-655c-45a5-8825-2c1ee18c6a5a","name":"Sky Ladder",
"colour":"black","grade":"V14","setOn":"2026-11-26","retired":false}
The other two end the same way, with 201 Created, a Location header, and the saved problem in the body. Your IDs and Date headers will differ.
Every value you sent is a string, and every field is present. That’s all toNewProblem() checks, so the route has no reason to refuse anything.
Once saved, these problems go out to everyone who asks. List the problems:
$ curl -i http://localhost:3000/problems
HTTP/1.1 200 OK
X-Powered-By: Express
Content-Type: application/json; charset=utf-8
Content-Length: 974
ETag: W/"3ce-CEt1bG8XK9uzEz6ZT2E/344AksM"
Date: Fri, 27 Nov 2026 10:19:15 GMT
Connection: keep-alive
Keep-Alive: timeout=5
[{"id":"1","name":"Chalk Dust",...},...,
{"id":"a4c52b50-655c-45a5-8825-2c1ee18c6a5a","name":"Sky Ladder",
"colour":"black","grade":"V14","setOn":"2026-11-26","retired":false},
{"id":"610cc086-4fd8-48a7-bb42-f8414f41e30e","name":"Early Bird",
"colour":"green","grade":"V3","setOn":"2027-06-15","retired":false},
{"id":"dbf58b0f-35ff-49a2-bcc8-e1e9c31e3a72","name":" ",
"colour":"yellow","grade":"V1","setOn":"2026-11-26","retired":false}]
The six starting problems are shortened here, and the three new ones are wrapped for display. They’re also in problems.json now, so they survive a restart. Every client that reads the list gets a V14 on a wall that tops out at V8, a problem from the future, and a name nobody can read. That includes Arjun’s page, the board by the wall, and any member’s script.
Arjun’s page never had a say when you ran curl. The server can’t reliably tell whether a request came from the page, from curl, from a script, or from a copy of the page that someone changed in their browser. Each one is an HTTP request with a method, headers, and a body. Headers like User-Agent or Origin say whatever the sender puts in them, so they can’t stand in for a check either.
The rule this course builds on is that only the server’s checks decide what reaches the data. Checks on a page help the person filling in the form, who learns about a mistake before sending anything. They protect nothing on the server.
If you’d like to explore who else could reach the API without the page, the mentor can work through it with you:
Find Where the Checks Belong
The route’s checks did exactly what they were written to do. They confirmed that each field is present and is a string, without inspecting its contents. A grade has to be one of the values V0 to V10, a date has to be a real date in the right format and no later than today, and a name has to contain something besides spaces.
Those are rules about ranges, formats, and blank values, and none of them is in the starting project. Query values have the same gap. The value in a query string like ?limit= arrives from the client as a string too, and the list route hands it to Number() without checking what it holds.
You could add more if statements to toNewProblem(). Then the handwritten checks would grow one field at a time, and every other route would need its own.
The check belongs at the boundary: the point where a request enters the server, before anything in it reaches the data. In an Express app, where should such a check sit so that every route gets it and no handler can forget it? To find more requests that slip past the current checks, try this with the mentor:
Leave the three new problems in problems.json. Later in the course, you’ll send the same three requests again and watch the API refuse them.
Check that you can find a gap in the starting API on your own and explain it:
- Mei says, “The page’s color dropdown only has yellow, blue, green, red, and black, so nobody can add a purple problem.” Write a
curlcommand that adds a purple problem, send it to your running API, and explain the response you get. - Tunde suggests fixing the V14 problem by tightening the grade dropdown on Arjun’s page. Why doesn’t that protect
problems.json?
Question 1: Your command sends a valid body with "colour":"purple", and the API answers 201 Created:
$ curl -i -H "Content-Type: application/json" \
-d '{"name":"Grape Escape","colour":"purple",
"grade":"V2","setOn":"2026-11-20"}' \
http://localhost:3000/problems
HTTP/1.1 201 Created
The headers and body are left out here. The API accepts the problem because the route only checks whether the colour field is a string, and "purple" is one. The dropdown limits what the page sends, not what the API accepts. Grape Escape is saved to problems.json too, so it’s a fourth stray problem. Leave it there with the other three.
Question 2: The API answers any client, not only the page. A curl command, a script, or a changed copy of the page can still send a V14. Only a check on the server decides what reaches problems.json.
You’ve seen the trusting API store a V14, a problem from next year, and a blank name, and you know the check has to sit at the server’s boundary. In the next lesson, Write Your First Middleware, you’ll write the kind of function Express runs for every request before it reaches a route.