Working with routes in ExpressJS: Working with query parameters in routes

Capítulo 55

Estimated reading time: 3 minutes

+ Exercise
Audio Icon

Listen in audio

0:00 / 0:00

One of the main characteristics of ExpressJS, a framework for Node.js, is the ease with which it allows the definition and manipulation of routes. Routes are essential in any web application as they define how client requests are handled by the server. In this chapter, we'll focus on how to work with query parameters in ExpressJS routes.

For starters, what are query parameters? These are values ​​that are passed in the URL of an HTTP request and are commonly used to filter results, page data, sort data, among other things. They are added to the URL after a question mark (?) and are separated by a & if there is more than one. For example, in the URL http://mysite.com/products?categoria=books&price=asc, "categoria=books" and "price=asc" are query parameters.

In ExpressJS, we can access these query parameters through the req.query object. For example, if we have the /products route and we want to filter products by category and sort by price, we could do something like this:

app.get('/products', function(req, res) {
  var category = req.query.category;
  var orderPrice = req.query.price;
  // Here you can use category and orderPrice to filter and sort your products
});

As you can see, req.query is an object that contains all query parameters as properties. You can access each query parameter individually, as we did above, or you can work with the req.query object as a whole, depending on your needs.

Also, it is important to note that query parameter values ​​are always strings. If you need them as another data type, you'll have to convert them. For example, if you have a query parameter "page" that must be a number, you could do something like this:

Continue in our app.
  • Listen to the audio with the screen off.
  • Earn a certificate upon completion.
  • Over 5000 courses for you to explore!
Or continue reading below...
Download App

Download the app

app.get('/products', function(req, res) {
  var page = parseInt(req.query.page, 10);
  // Now you can use page as a number
});

Another thing to consider when working with query parameters is that they are optional. If a customer makes a request for /products without any query parameters, req.query will be an empty object. Therefore, you should always check whether a query parameter exists before using it:

app.get('/products', function(req, res) {
  if (req.query.category) {
    var category = req.query.category;
    // Filter your products by category
  }
  if (req.query.price) {
    var orderPrice = req.query.price;
    // Sort your products by price
  }
});

In short, query parameters are a powerful tool to make your routes more flexible and interactive. They allow customers to customize their requests in a simple and intuitive way, and ExpressJS makes working with them a breeze. However, like any tool, they must be used with care. Always check that query parameters exist before using them, and don't forget that their values ​​are always strings.

I hope this chapter has given you a good overview of working with query parameters in ExpressJS. In the next chapter, we'll dig even deeper into ExpressJS routing, exploring how to work with dynamic routes.

Now answer the exercise about the content:

What are query parameters in ExpressJS and how are they used?

You are right! Congratulations, now go to the next page

You missed! Try again.

In ExpressJS, query parameters are values passed in the URL of an HTTP request. They are used to filter results, page data, sort data, and more. They follow a ? in the URL and use & to separate multiple parameters. For example, in http://example.com/search?category=books&sort=asc, both category and sort are query parameters.

Next chapter

Working with routes in ExpressJS: Validating data in routes

Arrow Right Icon
Free Ebook cover How to create APIs in NodeJS from basic to advanced
37%

How to create APIs in NodeJS from basic to advanced

5

(1)

149 pages

Download the app to earn free Certification and listen to the courses in the background, even with the screen off.