Module 3 ยท The server: Node, Express and the database Lesson 7 of 15
Your first API
An API is a set of URLs that return data instead of pages. Express makes routing them a few lines long.
The concept
An API is a set of URLs your server answers with data instead of pages. Express gives you four of them in a few lines: app.get('/api/jobs', โฆ) to list, app.post('/api/jobs', โฆ) to create, plus put and delete for edit and remove. Each handler reads the request and sends JSON back.
Keep the same shape in every response โ { data: [...] } on success, { error: 'message' } on failure โ and use the right status code: 200 for fine, 201 for created, 400 for a bad request, 401 for not signed in, 404 for missing, 500 for your own bug. Return the new record after a create so the client does not have to fetch again.
Common mistakes
The mistake is building the API and the front end at the same time. Finish three endpoints, test them with a browser tab or Postman, THEN connect the interface. Debugging two moving parts at once is how a two-week project becomes a two-month one.
Worked example
app.get("/api/jobs", (req, res) => res.json(jobs)) is a working endpoint; adding a POST that pushes to the same array makes it a working small service.
Your practice task
Build an API with four endpoints โ list, read one, create, delete โ backed by an array in memory first.
Do this before moving on. Reading a solution teaches the shape of it; typing it teaches you.
Key takeaway
An API is a set of URLs your server answers with data instead of pages.
Keep your place in this course
A free account saves which lessons you have finished and unlocks the certificate. The lesson itself stays free to read.
Create free account or sign inCourse complete
You finished every lesson.
Your certificate is ready, and the projects you built are the proof to attach to an application.
Almost there
15 lessons left of 15
Finish every lesson to unlock the certificate. Each one ends in a practice task โ do them and the final project brief writes itself.