Query Strings are not part of a route!

I’ve learned something today as a result of a question by Yuri (a classmate from Opensource class) on the class’ slack channel. He is implementing a web service for the google phone library using Node JS and Express JS. He asked if it was possible use one GET route that would check for data passed by the user as part of the last URI segment as well as a query string.


The callback function for the Express route should get the phone numbers for the following cases

case 1: localhost:3000/findPhoneNumber/1234567890

case 2: localhost:3000/findPhoneNumber/?q=1234567890

At first glance, I thought it would be possible since both are pointing to /findPhoneNumber end point. Theoretically, if this worked, code can be written to check for case 1 and case 2 under one route so code can be made more simple.

However, Yuri discovered that query strings are not considered to be part of a route.

This made my assumption incorrect. The routes are different because case 1 represents the path to resource “1234567890”. Case 2, on the other hand, represents a path to /findPhoneNumber/ end point only, hence the routes do not match.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s