Node JS Tutorial - Query Strings

In this tutorial, we will learn about Query Strings, which are the parts of a URL that come after the path and start with a question mark ('?').

Let's explore how to retrieve the properties and values of a query string.

Retrieving Query String Properties

Open the index.js file and create a new route:


app.get('/user',(req,res)=>{
var name = req.query.name;
var age = req.query.age;
res.send('Name:'+name + 'Age:'+age);
});

Now, let's test the route. Go to the command prompt and run the index.js file:


node index.js

Next, switch to your browser and navigate to the URL /user. Append the query string ?name=jenifer&age=25 to the URL.

You should see the values of the query string properties, name and age, displayed on the page.

This is how you can retrieve query string properties in Node.js.