Filter and Page the Ticket List

Dev’s desk can now look up, change, and delete any single ticket, but GET /tickets still sends every ticket the café has. On a busy Saturday, that’s dozens of toasters, jackets, and bikes, and most people only want a slice of them. Amara wants the electrical items still waiting for her, and the desk’s screen has room for ten tickets at a time.

As a client, you’ve added a ? and a few key=value pairs to a URL to ask an API for less. Now your server reads them. In this lesson, you’ll filter and sort the ticket list from the query string, send it one page at a time, and respond with a 400 to a query value you can’t use. First, find out what Express makes of a query string.

Read the Query String

Your endpoint table already promised /tickets?status=waiting&category=electrical for part of the list. Everything after the ? is the query string. It isn’t part of the path, so this request still reaches your GET /tickets route, and Express parses the query string into an object on req.query. To see that object, add this line at the start of the GET /tickets handler:

Language: TypeScript Filename: app.ts
console.log(req.query);

Save, and send three requests from your second terminal. Put each URL in double quotes, because an unquoted & tells your shell to run the command in the background:

Language: Shell
$ curl "http://localhost:3000/tickets?status=waiting&category=electrical"
$ curl "http://localhost:3000/tickets?status=waiting&status=fixed"
$ curl "http://localhost:3000/tickets?limit=2&sort="

Each request still gets every ticket back, because the route doesn’t use the query yet. The app’s terminal shows what arrived:

Language: Shell
[Object: null prototype] { status: 'waiting', category: 'electrical' }
[Object: null prototype] { status: [ 'waiting', 'fixed' ] }
[Object: null prototype] { limit: '2', sort: '' }

The [Object: null prototype] label is Node’s way of saying the object has no inherited properties, so the only keys in it are the ones the client sent. Those keys follow rules that your code has to handle:

  • A key sent once has a string value, even the 2 in limit=2, because everything in a URL arrives as text.
  • A key sent twice becomes an array of strings, such as [ 'waiting', 'fixed' ].
  • A key with nothing after it is an empty string, whether the URL says sort= or only sort.
  • A key the client didn’t send isn’t there, so req.query.offset is undefined.

Express 5’s default query parser doesn’t build nested objects, either. A URL with ?filter[status]=waiting gives you one flat key named 'filter[status]'. You may see bracket queries like that in older Express code, which parsed them into objects.

And req.query is a getter in Express 5, so assigning to it throws a TypeError. Read from it, and keep the values you’ve checked in your own variables.

Before you write code that handles these cases, try a few more of your own. Type a query string into the box below, or pick one of the requests from above, and read what console.log(req.query) prints, along with each key’s value and typeof. Try a key sent three times, a key with no =, and a limit of two:

Interactive diagram — enable JavaScript to view.

A third status makes the array three strings long, a key with no = gets the empty string '', and limit=two arrives as the string 'two', just as limit=2 arrives as '2'. Nothing has checked any of them yet. And every key you didn’t send, such as offset, reads as undefined.

If you’d like a partner for more of these, the mentor can quiz you:

Delete the console.log() line before you go on.

Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

Locked learning resources

The full lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

Become a Member to join the conversation.