Get Query Strings and Parameters in Express Routes on NodeJS

Robin
Updated on October 6, 2022

The query strings and parameters are used to accept information in an Express route through the URL. You can extract that information from the URL using req.query and req.params objects.

To get query strings, we use the req.query object inside the route. For route parameters, we need to use the req.params object.

You can also combine multiple query strings or parameters. Both methods have their own use cases. It is also possible to use the query and parameter together in the same route.

Let's look into both techniques and learn how we can get query strings and parameters from the URL inside an Express route.

Get Query Strings and Parameters in Express Routes on NodeJS

Also Read: All Steps to Get Full URL From Request in Express on Node.js

Getting Query String Using req.query in Express

A query string is a key-value pain that starts with the question mark (?) in the URL. Multiple query strings are joined together with the (&) sign. All query strings are available in the req.query object as its property. To get value access the key from this object.

We can easily add a query string to an URL using the question mark followed by the key and value. When a user visits that link, we need to extract the query value in order to give them the proper response.

The query strings are used for many reasons. You can use the value to retrieve data from a database or filter the data in a specific order depending on the query value. We can add multiple query strings to an URL.

Query strings in Node.js Express routes

You can use these values however you want inside the server. Let's see how you can get the value from the request object inside the Express route.

          app.get('/profile', (req, res) => {
    const username = req.query.username;
    // max

    const country = req.query.country;
    // usa

    res.send('Profile page');
});
        

In this /profile route, I am accepting two query strings i.e. username and country. That means, whenever users visit this route, they will provide these two queries. The URL will look like this:

URL – https://example.com/profile?username=max&country=usa

You can get these queries with their key username and country on the req.query object. The req.query.username will return max and req.query.country will return usa because the URL contains these values.

Now you can grab data from the database related to this username and send it as a response. When different users visit this route with different usernames and countries, they will get different data in their responses.

You can also get data from the request body in Express routes when a user sends a post request by submitting a form or using any other client.

          app.get('/profile', (req, res) => {
    const { username, country } = req.params;

    res.send('Profile page');
});
        

If you are using multiple query strings in your route, you can use the object destructuring syntax to get the values without calling req.query multiple times.

Getting Route Parameter Using req.params in Express

A parameter is an actual part of the URL path. It is like a variable whose value changes to get a different response from the URL. Express route defines the parameter name and gets the value from req.params object as a property for every request.

The parameters are used to pass data through the URL in the server. To accept a parameter, you have to explicitly define the name of that parameter in your route.

Route parameters in Node.js Express server

If I want to set up the "username" parameter in the /profile route, I have to add the name with the colon (:) symbol followed by the name of the parameter name in my route.

When I add the parameter /profile/:username in the route, I will be able to get the value inside that route using the req.params object.

Now when a user tries to visit the URL, it will look like this:

URL – https://example.com/profile/max

          app.get('/profile/:username', (req, res) => {
    const username = req.params.username;
    // max

    res.send('Profile page');
});
        

You can extract the value "max" by using req.params.username inside the route. I can use the value according to the requirements of my application.

Also Read: How to Set and Get Cookies in Node.js Express Server

Multiple Route parameters in The Express Server

In the previous section, you have seen how to access a parameter in Express. But an Express route can have multiple parameters as well. You have to add those parameter keys to the route.

I want to accept the "username" and "country" parameters in my /profile route. Therefore I will add both of them to my route like this – /profile/:username/:country

          app.get('/profile/:username/:country', (req, res) => {
    const username = req.params.username;
    // max

    const country = req.params.country;
    // usa

    res.send('Profile page');
});
        

I am accessing the "username" and "country" parameters with req.params.username and req.params.country respectively.

You can also use object destructuring syntax to get multiple values from the req.params object. In this way, we can access multiple object properties in a single line.

          app.get('/profile/:username/:country', (req, res) => {
    const { username, country } = req.params;

    res.send('Profile page');
});
        

In this way, we can use multiple parameters and get the values from the request object in the Express routes.

When Should You Use Query & Parameter?

The query strings and parameters carry information to the server through the URL. Both of them are used for the same purpose. But they have some differences.

Before choosing the query or parameter for your route, you should know the differences. I will help you to understand their use cases.

  • You don't need to change the route path to access query strings. But for parameters, you have to add them individually in the route.
  • You can access as many queries as you want in the route very easily because you don't have to add them to the route path. You just add them to the URL.
  • You need to add the parameter values in a specific order to the URL according to the route path. But you can put queries randomly, order doesn't matter.

Now you can decide which one you should use on the route. If the route needs a few values then the parameter will be a good choice.

But if the route needs many values and you don't want to follow the order, the query strings will be easy to use and manage.

Also Read: Perfect Ways to Serve Static Files in Express and Node.js

Conclusion

Express has easy ways to handle queries and parameters inside the routes. You can access these values by using the request object. Mainly we have two objects in the request.

  • req.query – Contains all the query strings as the object properties.
  • req.params – Contains all the route parameters as the object properties.

Using these two objects from the request, you can get the query strings and parameters in Express and Node.js servers.

Related Posts