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

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

Page 55 | Listen in audio

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:

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.

Article image Working with routes in ExpressJS: Validating data in routes

Next page of the Free Ebook:

56Working with routes in ExpressJS: Validating data in routes

4 minutes

Earn your Certificate for this Course for Free! by downloading the Cursa app and reading the ebook there. Available on Google Play or App Store!

Get it on Google Play Get it on App Store

+ 6.5 million
students

Free and Valid
Certificate with QR Code

48 thousand free
exercises

4.8/5 rating in
app stores

Free courses in
video, audio and text