DevPath · Learn to code ESPTEN

Backend, HTTP and handlers

Routes, parameters and REST

Route parameters

Routes can have dynamic parameters, marked with :. They arrive in req.params:

app.get("/products/:id", (req, res) => {
  const id = req.params.id;       // if the route is /products/5 -> "5"
  res.json({ id });
});

The request body

In POST/PUT the data arrives in req.body:

app.post("/tasks", (req, res) => {
  const title = req.body.title;
  res.status(201).json({ id: 1, title });
});

REST in one line

A REST API models resources (users, products...) and uses the HTTP method for the action: GET /products (list), GET /products/5 (view one), POST /products (create), PUT /products/5 (update), DELETE /products/5 (delete).

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 →
← Express: handlers (req, res)View the module →