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:
req(request): the request. It containsreq.params,req.query,req.body,req.headers...res(response): the response. It's used to reply:
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(...)andcrearRes()) and check what it responds (res.statusCode,res.body).