DevPath · Learn to code ESPTEN

Backend, HTTP and handlers

Express: handlers (req, res)

Express

Express is the most popular backend framework in Node. It maps a route to a handler function:

app.get("/greeting", (req, res) => {
  res.json({ message: "Hello" });
});

The handler receives two objects:

res.json({ ... });          // responds with JSON (status 200 by default)
res.status(404).json({ ... }); // sets the status code and responds
res.send("text");           // responds with text

In these exercises you write the handler. In the tests we call it with a simulated request and response (crearReq(...) and crearRes()) and check what it responds (res.statusCode, res.body).

Put this into practice

DevPath is a hands-on course: you read the theory here; in the app you put it into practice with exercises that really run, offline.

Start free in the app →
← The backend and HTTPRoutes, parameters and REST →