Up Next: Data with MongoDB
See how much of an API you can build after Module 2, meet the Library Loans API project where you'll show it, and find out why Module 3 moves an API's data into MongoDB.
Apprentice · Node.js · REST API · Updated
After this lesson you can
- Choose the Module 2 tool that fits an API job, from route and query parameters to middleware, validation, error handlers and Supertest
Key takeaways
- After Module 2 you can build a REST API with Express, with routes, parameters, middleware, validation and one error shape, and prove it works with node --test and Supertest.
- The Library Loans API project is a library's book loans, built with everything from Module 2 and tested on every endpoint and error.
- Each API job has its tool, such as a route parameter for an id, a query string for a filter, middleware for every request, Zod for a body, and Supertest for the proof.
- Data kept in memory is gone each time the server restarts. Module 3 keeps it in MongoDB, so what users save is still there tomorrow.
At the start of this module, a server was a function that answered every request the same way. Now you can build the kind of API that sits behind a phone app's login, shopping cart or ticket sales: it reads what each request asks for, checks what it's sent, answers with the right status code and a clear error, and has tests that prove all of it in a second. That's the everyday work of a back-end developer.
This lesson looks back at what you can do, shows you the project where you'll put it together, and shows you the one thing every API you've built so far forgets.
What you can do now
After Module 2, you can:
- read HTTP: a request's method, path, query string, headers and body, and a response's status code, headers and body;
- answer requests with Node's own
node:http, and see what a framework takes over; - build an Express app with routes, route parameters (
/shows/:id, read "colon id") and query parameters; - run code on every request with middleware, and give each resource its own router;
- design REST endpoints with nouns for paths and methods as verbs, and the status code each action returns;
- check what comes in, by hand and with a Zod schema, and answer 400 with every problem at once;
- handle errors in one place, with one JSON error shape for the whole API and never a stack trace for a caller;
- prove it all works with
node --testand Supertest, errors included, with a fresh start for every test.
Show what you can do: the Library Loans API
A small neighborhood library lends books and keeps track of who has what. Module 2's project is that library's API, the Library Loans API:
- Books: list them, search by title or author with a query string, add one, update one, and remove one.
- Loans: borrow a book, return it, see a member's current loans, and list the loans that are overdue.
- Rules and errors: a Zod schema for every body, a 400 that lists every problem, a 404 for a book that isn't there, and a 409 for a book that's already out.
- Organized and proven: a router for books and one for loans, an
.httpfile of requests to try, and Supertest tests for every endpoint and every error.
The data lives in memory, as in this module's exercises. When you can show a friend the whole API working from REST Client, and every test green, you've built a real back end.
Which tool fits?
Most of the project, like most API work, comes down to one question at each step: which tool does this job?
| The job | The tool |
|---|---|
Read the id in /books/42 |
A route parameter, /books/:id, and req.params.id |
Filter a list, such as ?title=garden |
The query string, req.query |
| Read the JSON a client sent | express.json(), then req.body |
| Run the same code before every request, such as timing it | Middleware, (req, res, next) |
| Keep each resource's routes together | express.Router(), one router per resource |
| Turn away a body with missing or wrong fields | A Zod schema and safeParse, answering 400 |
| Answer every error with the same JSON shape | Error-handling middleware, (err, req, res, next) |
| Prove an endpoint answers the right status and body | A Supertest test, request(app) |
Try it. For each endpoint of the Library Loans API, write down the tools from the table you think you'll need. You'll find out how close you were when you build it.
The one thing these APIs forget
Every API in this module keeps its data in memory, in an array. Buy 2 tickets from Starlight Stage's API, then ask for the show. Each line below is a request, its status code and the body that came back:
POST /shows/2/tickets 201 {"id":1,"showId":2,"quantity":2,"total":66}
GET /shows/2 200 {"id":2,"title":"Midnight Brass Band","date":"2026-12-05","price":32,"seatsLeft":2}
Now stop the server with Ctrl+C, start it again, and ask for the show once more:
GET /shows/2 200 {"id":2,"title":"Midnight Brass Band","date":"2026-12-05","price":32,"seatsLeft":4}
The 2 seats are back on sale, and the fan's ticket is gone. Memory lasts only as long as the program runs: a restart, a crash or a new version of the code, and every sale disappears. In tests, a fresh start is exactly what you want. In a real venue, it would sell the same seat twice.
What Module 3 unlocks
Module 3, Data with MongoDB, keeps the data:
- The MongoDB you need for Node: documents and collections, a database on your own computer, and the official driver saving and reading documents.
- Reading and changing data: filters, sorting and paging, updates and deletes, indexes and text search.
- An API backed by MongoDB: database code kept in one place, wired into Express, and tested with Supertest against a throwaway database that starts for the tests and disappears after, so the fresh start you met in this chapter carries on.
Its project is the Recipe Box API: an API that keeps your recipes and finds one by an ingredient, and still has them tomorrow.
Check yourself
2 questions to try on the lesson page.
So what
You can build an API and prove it works, and Module 3 makes sure what its users save is still there after a restart.