APIs with Express · Chapter 5 of 29

The response

By the end of this chapter you can answer with the right status code and headers: 201 with a Location, 204, redirects, a format the client asked for, and the 4xx that says what went wrong. 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

The partner app team at Third Wave writes to Kabir: every answer from Tapri is a 200, even when an order fails, so their app shows "Order placed!" for orders that never existed. They also want to know where a new order lives without guessing its URL. And an old link on Tapri's Instagram, /chai, leads nowhere.

Programs do not read the body first, they read the status line and the headers. So far every route answers 200 with res.json, and an error in the body is invisible to the partner's code.

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: 201 Created, with a Location

    Inside createApp, add POST /orders. It reads a JSON body { item, qty }. For an item on the menu it adds the order { id, item, qty, status: 'preparing' } to the orders, with ids going on from 3, and answers 201 with that order and the header Location: /orders/<id> (Location: /orders/3 for the first one). A GET on that Location answers the same order.

    An item that is not on the menu answers 400 with { error: 'Unknown item: <item>' }, and that answer has no Location header.

  2. Task 2

    DELETE /orders/:id: 204 and nothing else

    Inside createApp, add DELETE /orders/:id. It removes the order and answers 204 with an empty body. Afterwards GET /orders/<id> answers 404, and the other orders are still there.

    An id that is not digits only answers 400 with { error: 'Order id must be a whole number' }, and an unknown order, one already deleted included, answers 404 with { error: 'No order <id>' }. findOrder in createApp already sends both.

    The response must end: the check gives up after 1.5 seconds.

  3. Task 3

    /chai and /today: a permanent and a temporary redirect

    Inside createApp, add two redirects. GET /chai, an old Instagram link that moved for good, answers 301 with Location: /menu. GET /today answers 302 with Location: /menu/<todaysSpecial> (today that is /menu/ginger-chai), because the special changes every day.

  4. Task 4

    GET /menu: JSON or plain text, whichever the client accepts

    Inside createApp, add GET /menu. It always sends the header Cache-Control: public, max-age=300, and picks the format from the Accept header.

    Accept: application/json answers 200 with the menu as JSON, and so does Accept: / (any type), so JSON must win when the client takes anything. Accept: text/plain answers 200 with a Content-Type of text/plain and one line per item, joined with a newline and with none at the end: 'Masala chai: ₹40\nCutting chai: ₹25\nGinger chai: ₹35\nSamosa: ₹20'. Anything else, such as application/xml, answers 406 with { error: 'Tapri answers JSON or plain text' }.

    The answer also carries Vary: Accept, which Express adds when it picks the format for you.

  5. Task 5 · interview question

    Interview: POST /orders/:id/cancel, 400, 404 or 409

    Fix POST /orders/:id/cancel. A cancel that works sets the order's status to 'cancelled' and answers 200 with the order: POST /orders/2/cancel answers { id: 2, item: 'samosa', qty: 3, status: 'cancelled' }. Today every failure answers 200 too, so partner apps think it worked. Each failure needs its own status:

    400 with { error: 'Order id must be a whole number' } for an id that is not digits only (abc, 1e0, 2.0). 404 with { error: 'No order <id>' } for an unknown order, such as 99. 409 with { error: 'Order <id> is already <status>' } for an order that is no longer preparing: { error: 'Order 1 is already served' }, and cancelling order 2 twice gives { error: 'Order 2 is already cancelled' }.

What you will learn

  • res.status: set the code, then end the response with a body or without one.
  • 201, Location and 204: what a create and a delete answer.
  • res.redirect: 302 for a temporary move, 301 for a permanent one.
  • res.format: one URL, several formats, picked by Accept.

More in First routes

Build The response 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.