APIs with Express · Chapter 3 of 29

Params and query strings

By the end of this chapter you can read values from the path and the query string, turn them into numbers safely, and answer 400 or 404 when they are 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

Partner cafés now place orders every morning, and the questions follow. Blue Tokai wants to check one of its own orders. Anjali wants the app to show only snacks under ₹25, cheapest first. Ravi wants to split a table's bill at the stall between however many people sat there.

With fixed paths you would need a route for every order (/orders/1, /orders/2, and so on) and one for every price limit. The URL has to carry the details: the path says which thing, the query string says how you want it.

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 /menu/:id: one item by its id

    Inside createApp, add GET /menu/:id. It answers 200 with the menu item whose id is the id in the path: GET /menu/ginger-chai answers the Ginger chai item.

    For an id that is not on the menu it answers 404 with { error: 'No item called <id>' }, naming the id as Express decoded it: GET /menu/vada%20pav answers { error: 'No item called vada pav' }.

  2. Task 2

    GET /cafes/:cafe/orders/:orderId: one café's order

    Inside createApp, add GET /cafes/:cafe/orders/:orderId. It answers 200 with the order whose id is orderId and whose cafe is cafe: GET /cafes/blue-tokai/orders/3 answers order 3.

    When no order has both, it answers 404 with { error: 'No order <orderId> for <cafe>' }. Order 3 exists but is Blue Tokai's, so GET /cafes/third-wave/orders/3 answers { error: 'No order 3 for third-wave' }.

    The ids in the orders array are numbers; orderId arrives as text.

  3. Task 3

    GET /menu: filter and sort from the query string

    Inside createApp, add GET /menu. With no query string it answers 200 with the whole menu. Three query keys, each optional, combine:

    category keeps the items of that category (?category=snack). maxPrice keeps the items costing at most that many rupees (?maxPrice=25; ?maxPrice=0 answers []). sort may only be price, and puts the cheapest first; without sort, the items stay in menu order.

    maxPrice must be a whole number written in digits only. Anything else, such as cheap, 25.5, -5, 1e2, an empty value, or the key given twice, answers 400 with { error: 'maxPrice must be a whole number' }. Any sort other than price answers 400 with { error: 'sort can only be price' }.

    Sorting must not change the order of the exported menu for the requests that come after.

  4. Task 4 · interview question

    Interview: GET /orders/:id, the lookup that never finds order 2

    Fix GET /orders/:id. GET /orders/2 must answer 200 with order 2; today it answers 404. A whole number that matches no order answers 404 with { error: 'No order <id>' }, for example GET /orders/99.

    The id must be digits only. Anything else answers 400 with { error: 'Order id must be a whole number' }, including texts a plain conversion turns into a real number: abc, 2abc, 1e0, 2.0, 0x2, %202 (a space and a 2) and -1.

  5. Task 5

    GET /bill/:table{/:people}: split a table's bill

    Inside createApp, add GET /bill/:table{/:people}, one route whose last segment is optional. bills maps a table number to its total in rupees. The answer is 200 with { table, total, people, each }, all four numbers, where each is the total divided by people, rounded up: GET /bill/7/2 answers { table: 7, total: 125, people: 2, each: 63 }. people defaults to 1, so GET /bill/4 answers { table: 4, total: 300, people: 1, each: 300 }.

    people must be a whole number from 1; anything else (0, two, 1.5) answers 400 with { error: 'people must be a whole number from 1' }. A table with no bill answers 404 with { error: 'No bill for table <table>' }, for example GET /bill/9. A name every object has, such as constructor, is not a table either.

What you will learn

  • Route params: a :name segment matches any value and lands in req.params.
  • Everything is text: why 2 === req.params.id is false, and why Number accepts too much.
  • Query strings: req.query, keys that are missing, empty or repeated.
  • Optional segments: Express 5's {/:name}.

More in First routes

Build "Params and query strings" 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.