๐Ÿ“Š We use cookies & analytics to improve your experience. Learn more
Latest

Full-Stack Web Development Bootcamp Module 3 ยท Lesson 7 of 15

0% Exit to course

Module 3 ยท The server: Node, Express and the database Lesson 7 of 15

Your first API

Learning objective

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.

Create free account or sign in

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.

Next recommended course Data Analytics with SQL & Excel