express get query parameters

To get query parameters in Express, you can use the req.query object. Here are the steps to do it:

  1. Define a route in your Express application using the app.get() method. For example, to handle a GET request to the path '/users', you can write:
app.get('/users', (req, res) => {
  // Code to handle the request
});
  1. Inside the route handler function, you can access the query parameters using the req.query object. The req.query object contains a property for each query parameter, where the property name is the parameter name and the value is the parameter value. For example, if the request URL is '/users?name=john&age=25', you can access the 'name' and 'age' parameters like this:
app.get('/users', (req, res) => {
  const name = req.query.name;
  const age = req.query.age;
  // Code to handle the request
});
  1. You can now use the query parameters in your code to perform the desired logic or send a response. For example, you can use them to filter data from a database or generate a dynamic response based on the parameters. Here's an example of sending a response with the query parameters:
app.get('/users', (req, res) => {
  const name = req.query.name;
  const age = req.query.age;

  res.send(`Hello ${name}! You are ${age} years old.`);
});

In this example, when a GET request is made to '/users?name=john&age=25', the response will be "Hello john! You are 25 years old."

That's it! You have successfully accessed query parameters in Express. Remember to replace '/users' with your desired route path and modify the code inside the route handler according to your application's logic.