Path and Query Parameters in Express.js

Learn about path and query parameters and how to access them in Express.js.

Now that we understand how Express.js works and handles incoming requests, let’s take the next step closer to understanding path (route) parameters and query parameters. Path and query parameters are important aspects of API development. Some of the popular use cases of these parameters are mentioned below:

  • Resource identification and filtering: Path parameters can be used to identify specific resources in an API, while query parameters can be used to filter resources based on specific criteria. For example, in an e-learning application, a path parameter could be used to identify a specific course by its ID, while query parameters could be used to filter courses based on category or price range.

  • Pagination and sorting: Path parameters can be used to paginate large datasets, while query parameters can be used to sort data based on a specific attribute. For example, in an e-commerce application, a path parameter could be used to specify the page number and the number of results per page, while query parameters could be used to sort products by popularity or price range.

  • Search and filtering: Path parameters can be used to identify the type of resource being searched, while query parameters can be used to filter search results based on specific criteria. For example, in an online food ordering application, a path parameter could be used to identify the type of food (e.g., ingredients, restaurant location, time to deliver), while query parameters could be used to filter search results by ingredient or dietary restriction.

Let’s now delve into path and query parameters and build the functionalities mentioned below:

  • The API will accept GET requests to fetch the details of a particular user with a userId. The value of userId will be a path (route) parameter.

  • The API will accept another GET request to list the details of NN users.

First, let’s understand the theory part behind path parameters and the query parameters.

What are path parameters?

Path parameters help to call an API specifically to a particular resource or route. Because we specifically route to a certain resource, we don’t need to build a body that will be sent with the request to find a particular resource, reducing the request's execution time. Path parameters are surrounded by curly brackets, which help developers control the representation of a specific resource in our application. A path parameter is a string that appears before a query string and is within the path of an endpoint. It provides an efficient way to parameterize a resource.

Path parameters are read from the URL using the params property available on the request object, which is passed as a parameter to the callback function. For building the functionalities above, we’ll use the get() method and access the value of userId to return the details of that user. We’ll create dummy data for this purpose. Let’s now jump right into the code.

Get hands-on with 1200+ tech skills courses.