Backend

Node.js: Build the server

Write real APIs with Node.js and Express: routes, JSON, data and security.

7 chapters · 24 lessons · free

Try a free lesson Create your free account

What you will learn

1. Your first server

What a server is, how a request becomes an answer, and how to start one with Express.

  • Hello from the server Try it A server receives requests and sends responses. app.get(path, (req, res) => ...) answers one address.
  • Start listening Exercise app.listen(port, callback) starts the server. The callback runs once it is ready.
  • A second page Exercise One app.get per route. Express checks the routes in order and the first match answers.
  • Cannot GET /hello Debug 404 means no route matched the address. Paths must match exactly.
  • Quick check Quiz Requests, responses, routes, ports and status codes: the basics of every backend.

2. Send JSON

Apps don't want web pages from an API, they want data. res.json() sends it, in exactly the format you learned.

  • Your first API Exercise res.json(object) sends data as JSON, with the header that says so (application/json).
  • A list from the server Exercise Keep the data in a variable and send it with res.json. Later, the variable becomes a database.
  • Two answers to one question Debug Every request gets exactly one response. Send it once.
  • Quick check Quiz res.json sends data, one response per request, and the data can be any object or list.

3. Routes with parameters

One route for every player: /api/hello/:name, ?search=pizza, and the 404 for things that do not exist.

  • Hello, anyone Exercise :name in a path is a parameter, and req.params.name holds its value.
  • A search API Exercise req.query holds the values after ?. filter keeps the matching items.
  • Found, or 404 Exercise res.status(code) sets the status. return stops the function so it answers only once.
  • The game that was always missing Debug Route parameters and query values are always text. Convert them with Number() before comparing to numbers.
  • Quick check Quiz :params and ?query, both as text, and a clear 404 when something is missing.

4. Receive data with POST

Sign-ups, new posts, high scores: apps send data to the server. Read it, check it, save it, and answer 201 or 400.

  • Echo what you get Exercise app.use(express.json()) reads JSON bodies, and req.body holds the data. app.post answers POST requests.
  • The body that never arrived Debug No express.json(), no req.body. And a crash inside a route becomes a 500.
  • Save a high score Exercise Validate the body, answer 400 for bad input, 201 for something created.
  • Quick check Quiz POST + express.json() + req.body, and status codes that tell the truth: 201, 400, 500.

5. A full REST API

Read, create, update and delete: a complete to-do API, with middleware that logs every request and protects private routes.

  • A to-do API Exercise Same address, different methods: GET reads, POST creates, DELETE removes. 204 means "done, nothing to send back".
  • Middleware that logs Exercise Middleware runs for every request before the routes. next() continues to the next step.
  • Protect the admin route Exercise Middleware on a single route guards it. 401 means "who are you? you are not allowed".
  • Quick check Quiz REST: addresses for things, methods for actions. Middleware for logging and security.

6. Backend exam

Ten questions about servers, routes, JSON and REST. Pass with 8 right answers.

  • The backend exam Exam You can build, test and secure a real API with Node.js and Express.

7. Final project

Build a real API for a topic you love, test it with requests, and send it for review.

  • Your own API Final project List, read one, create, and a correct 404: the core of every real API.

How the course works

You learn by doing: each lesson explains one idea, then you write real code in the editor and see the result right away. The first lesson is free to try without an account; sign up to save your progress and continue.

  • Try it

    Change working code and see the result immediately.

  • Exercise

    Complete a task. A checklist confirms each requirement as you type.

  • Debug

    Fix code that is broken on purpose, and learn why it failed.

  • Quiz

    Check your understanding at the end of a chapter.