expressjs
  1. expressjs-route-parameters-and-query-strings

ExpressJs Route Parameters and Query Strings

Syntax

Here is the syntax for Route Parameters and Query Strings in ExpressJs:

Route Parameters

app.get('/:id', (req, res) => {
  const id = req.params.id;
  res.send(`The id you requested is ${id}`);
});

Query Strings

app.get('/search', (req, res) => {
  const searchTerm = req.query.term;
  res.send(`The search term you entered is ${searchTerm}`);
});

Example

Let's suppose we have an API endpoint where we can get a user's information by their ID.

Route Parameters Example

app.get('/user/:id', (req, res) => {
  const id = req.params.id;
  const user = users.find(user => user.id === id);
  if (user) {
    res.send(user);
  } else {
    res.status(404).send('User not found');
  }
});

With this code, when we make a GET request to the endpoint /user/1, we will receive the information of user with ID 1.

Query Strings Example

Let's say we have an endpoint that takes a query param name and returns a list of users with matching names.

const users = [
  {id: 1, name: 'Alice'},
  {id: 2, name: 'Bob'},
  {id: 3, name: 'Charlie'}
];

app.get('/users', (req, res) => {
  const nameToSearch = req.query.name.toLowerCase();
  const filteredUsers = users.filter((user) => user.name.toLowerCase().includes(nameToSearch));
  res.send(filteredUsers);
});

Now when we make a GET request to the endpoint /users?name=al, we will receive a list of all the users whose names contain the letters 'al' (in this case, just Alice).

Explanation

In ExpressJS, route parameters and query strings are two ways to pass parameters in an HTTP request.

Route parameters are part of the URL path. They are denoted by a colon : followed by the parameter name. Route parameters are used to identify a specific resource. For example, /user/:id would match the URL /user/1.

Query strings are appended to the URL and separated by a question mark ?. They consist of key-value pairs separated by an equals sign = and can be separated from each other by an ampersand &. Query strings are used to add additional information to a request. For example, /search?term=expressjs would search for the term "expressjs".

Use

Route parameters and query strings can be used to:

  • Retrieve information from a server
  • Filter, sort, or search a collection of data
  • Modify or update a resource

Important Points

  • Route parameters are part of the URL path and are denoted by a colon :.
  • Query strings are appended to the URL and are denoted by a question mark ?.
  • Route parameters are used to identify a specific resource. Query strings are used to add additional information to a request.
  • To access route parameters in ExpressJS, use req.params.
  • To access query strings in ExpressJS, use req.query.

Summary

ExpressJS route parameters and query strings are useful tools for passing parameters in an HTTP request. Route parameters are used to identify a specific resource while query strings are used to add additional information to a request. To access route parameters and query strings in ExpressJS, use req.params and req.query, respectively.

Published on: