APIs with Express · Chapter 22 of 29

What changed in Express 5

By the end of this chapter you can write routes the Express 5 way and move old Express 4 code across without surprises. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.

  • Part: Production
  • 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

Anjali still wants the receipts and feedback routes a freelancer wrote for Tapri two years ago, on Express 4. Kabir pastes them into the new app, and the server will not even start:

PathError [TypeError]: Missing parameter name at index 11: /receipts/*; visit https://git.new/pathToRegexpError for info

Most of what you know still works, but Express 5 changed a few things on purpose. Some fail loudly like this one. Others fail quietly: a status that comes out as 200, a number that stays a string, a crash on an empty request. Old tutorials are full of them, and interviewers love them.

What you build: 5 tasks

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

  1. Task 1

    GET /orders/:id: async errors reach the error handler

    In createApp({ store }), add GET /orders/:id as an async route. It awaits store.find(id) with the id from the path, as a string ('7'), and answers the order it gets as JSON with status 200.

    When find gives back nothing, the route throws an error with status 404 and message 'No such order'. When find rejects (the database is down), the rejection must reach the error handler too. Use no try and catch: the check gives up after 1.5 seconds.

    Register an error handler after the routes. Its status is the error's status, or 500 when it has none. Below 500 it answers { error: <the error's message> }, so a missing order gets 404 { error: 'No such order' }. From 500 up it answers { error: 'Something went wrong' } and never the real message.

  2. Task 2

    GET /files and GET /menu: the new path syntax

    Add GET /files/ followed by any path of one or more segments. It answers { path } with the segments, decoded, joined by '/': /files/menus/2026/oct%20special.pdf answers { path: 'menus/2026/oct special.pdf' } and /files/logo.png answers { path: 'logo.png' }. /files alone answers 404.

    Add GET /menu with an optional category segment. /menu answers the whole menu, /menu/snacks only the items whose category is 'snacks' (the bun maska), and a category with no items (/menu/biscuits) answers [].

  3. Task 3

    GET /search: tags, a flat key and a page that stays a number

    Add GET /search, answering { tags, maxPrice, page }.

    tags is always an array of the tag query values: ?tag=ginger&tag=strong gives ['ginger', 'strong'], ?tag=ginger gives ['ginger'], and no tag gives [].

    maxPrice is the price[max] query value as a number (?price[max]=50 gives 50), or null without it.

    page is a whole number of at least 1, default 1: ?page=3 gives 3, and 0, -2, two, 1.5 or no page give 1. The paging middleware is meant to work this out, but the route still sees a string: fix it, and use it for this route.

  4. Task 4

    POST /orders: no JSON body is a 400, not a crash

    Add POST /orders. With no body at all, or a body that is not JSON (text/plain, for example), it answers 400 with { error: 'Send the order as JSON' }.

    When items is missing, is an empty array or is not an array ({}, { items: [] } or { items: 'chai' }), it answers 400 with { error: 'An order needs items' }.

    Otherwise it answers 201 with { id, items }: the ids count 1, 2, 3 in the order the orders arrive, starting at 1 for each createApp().

  5. Task 5 · interview question

    Interview: legacyRouter, the Express 4 router on Express 5

    Fix legacyRouter(), the old Express 4 routes, so that on Express 5 it answers:

    GET /receipts/ with one or more segments: { file } with the segments joined by '/' (/receipts/2026/10/order-7.pdf gives { file: '2026/10/order-7.pdf' }), and /receipts alone is 404.

    GET /cafes: { city: 'all' }. GET /cafes/bengaluru: { city: 'bengaluru' }.

    POST /feedback with { name: 'Anjali' }: 201 with { thanks: 'Anjali' }.

    DELETE /feedback/3: 204 with an empty body.

    Today the router throws before it can be mounted. Two lines fail with no error at all: they answer 200 instead of 201 and 204.

What you will learn

  • Async errors: a handler that throws or rejects reaches your error handler, with no try and catch.
  • The new path syntax: named wildcards and optional parts in braces.
  • req.query and req.body: the simple query parser, a query that is parsed again on every read, and a body that starts undefined.
  • Removed methods: what is gone, and what fails without an error.

More in Production

Build "What changed in Express 5" 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.