APIs with Express · Chapter 4 of 29

The request

By the end of this chapter you can read a JSON or form body, check which format a client sent, read headers, the method and the caller's address, and pick the right URL under a mount. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.

  • Part: First routes
  • 5 tasks
  • Express
  • In the all labs plan

The first 2 chapters of APIs with Express are free. This one comes with the all labs plan.

Why you need this

Orders now arrive from three partners, and each sends them differently. One sends JSON, one sends plain text "because it worked in Postman", and the tablet at the stall still posts the old HTML feedback form. Every morning Ravi asks the same question about a strange order: which café sent it, and from where?

Everything a client tells you is on req, but Express does not read a body unless you ask: req.body is undefined, and the first line that reads it crashes the request with a 500. And a form value or a header is whatever the client chose to send.

What you build: 5 tasks

Each task has tests. You write the code in the lab's editor and press Check.

  1. Task 1

    POST /orders: an order from a JSON body, with its total

    Inside createApp, add POST /orders. It reads a JSON body { item, qty }, where qty is optional and defaults to 1. It answers 201 with { id, item, qty, total }: total is the item's menu price times qty, and ids go 1, 2, 3 in each app. { item: 'masala-chai', qty: 3 } answers { id: 1, item: 'masala-chai', qty: 3, total: 120 }.

    An item that is not on the menu answers 400 with { error: 'Unknown item: <item>' }. A qty that is not a whole JSON number of at least 1 (0, -2, 1.5, or the text '2') answers 400 with { error: 'qty must be a whole number from 1' }.

    A rejected order uses no id: the next good order still gets the next number.

  2. Task 2

    POST /orders: JSON only, or 415

    POST /orders answers 415 with { error: 'Send the order as JSON' } unless the request's Content-Type is JSON. A plain text body, a form body and a request with no body at all are all turned away. application/json; charset=utf-8 is still JSON and is accepted.

    Check the format before you read the body: for anything but JSON the body is undefined.

  3. Task 3

    /whoami: what the server sees about the caller

    Inside createApp, answer /whoami for every method (GET, POST, DELETE and the rest) with 200 and { cafe, method, ip }.

    cafe is the X-Cafe request header, in whatever case the client writes its name (X-CAFE, x-cafe), or 'walk-in' when there is none. method is the request's method, such as 'DELETE'. ip is the address the request came from (in the lab, ::ffff:127.0.0.1 or similar).

  4. Task 4 · interview question

    Interview: /partners, path, baseUrl and originalUrl under a mount

    Inside createApp, mount one handler at /partners that answers every method, for /partners and every path below it, with 200 and { baseUrl, path, originalUrl, query } read from the request.

    GET /partners/blue-tokai/orders?status=ready answers { baseUrl: '/partners', path: '/blue-tokai/orders', originalUrl: '/partners/blue-tokai/orders?status=ready', query: { status: 'ready' } }. POST /partners answers { baseUrl: '/partners', path: '/', originalUrl: '/partners', query: {} }.

    A mount matches whole segments only, so /partnership is not under it and answers 404.

  5. Task 5

    POST /feedback: the old form, with the rating as a number

    Inside createApp, add POST /feedback. The tablet at the stall sends an HTML form body (application/x-www-form-urlencoded) with name and rating, such as name=Anjali&rating=4. It answers 201 with { name, rating }, where rating is a number: { name: 'Anjali', rating: 4 }. A name that is missing or empty becomes 'Anonymous'.

    rating must be one of 1, 2, 3, 4 and 5. Anything else (0, 6, great, 4.5, an empty value) answers 400 with { error: 'rating must be 1 to 5' }.

What you will learn

  • Body parsers: express.json() and express.urlencoded() read a body onto req.body, each for its own format.
  • req.is: which format the client says its body is in.
  • Headers, method and address: req.get, req.method, req.ip.
  • Under a mount: req.baseUrl, req.path and req.originalUrl.

More in First routes

Build The request yourself

Partner cafés want Tapri’s menu online by Friday: you build its API. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.

The first 2 chapters of APIs with Express are free. This one comes with the all labs plan.