Express.js Tutorial: How to Access and Manage GET Query Params

Explore how to work with GET query parameters in Express.js, from accessing and handling them to applying best practices and debugging effectively.

Get Started free
Guide Banner Image
Home Guide Express.js Tutorial: How to Access and Manage GET Query Params

Express.js Tutorial: How to Access and Manage GET Query Params

Query parameters are a simple yet powerful way to pass information through URLs in an API. In Express.js, they are commonly used for features like filtering results, managing pagination, or toggling options without altering the route structure itself.

Overview

In Express.js, GET query parameters are accessed using req.query. They let you pass key–value pairs in the URL (e.g., /search?q=node&limit=10) to control API behavior without modifying the route.

Best Practices for Handling Query Params

  • Validate inputs with libraries like express-validator or Joi
  • Handle missing or invalid params gracefully (fallbacks or error messages)
  • Keep query params short and meaningful (limit, sort, q)
  • Avoid sensitive data in query strings (use headers or body instead)
  • Document supported query params for easier API usage
  • Test with edge cases (empty values, special characters, large numbers)

This article explores how to work with query parameters in Express.js, from understanding and accessing them, to handling multiple or optional values, avoiding pitfalls, and best practices.

Understanding Query Parameters

Query parameters are key–value pairs that appear in the URL after a ? symbol. They allow clients to send additional information to the server without changing the route itself. Multiple parameters are separated by &.

For example:

/users?role=admin&active=true&page=2

In this URL:

  • role=admin → specifies the type of user
  • active=true → filters only active users
  • page=2 → requests the second page of results

In Express.js, query parameters are retrieved through the req.query object, which automatically parses them into a JavaScript object:

app.get('/users', (req, res) => {

  console.log(req.query);

  // { role: 'admin', active: 'true', page: '2' }

  res.send('Query parameters received!');

});

This makes query params especially useful for search filters, pagination, sorting, and feature toggles in APIs.

Accessing Query Params in Express.js

Express makes it straightforward to read query parameters using the req.query object. Each query parameter from the URL is automatically parsed into a JavaScript object that you can use inside your route handler.

Example

app.get('/search', (req, res) => {

  const { q, limit } = req.query;

  res.send(`Search term: ${q}, Limit: ${limit}`);

});

Request:

/search?q=node&limit=5

Response:

Search term: node, Limit: 5

If a parameter isn’t provided in the URL, it will simply be undefined in req.query.

Example:

app.get('/products', (req, res) => {

  console.log(req.query);

  res.json({ filters: req.query });

});

Request:

/products?category=books&sort=asc

Console output:

{ category: 'books', sort: 'asc' }

This makes req.query ideal for handling filters, sorting, search terms, and pagination controls in APIs.

Workflow:

  • A client sends a request URL with query parameters (e.g., /search?q=node&limit=5).
  • Express matches the request to the correct route (/search).
  • The part after the ? is automatically parsed into key–value pairs.
  • These pairs become available inside the route as query parameters (q = “node”, limit = “5”).
  • The server uses those values to build a response and sends it back to the client.

HTTP Interceptor Banner

Common Pitfalls & Deprecated Methods

While working with query parameters in Express.js, developers often run into a few avoidable issues and sometimes rely on outdated methods.

Common Pitfalls

  • Assuming all parameters exist: If a query parameter is not provided in the URL, its value will be undefined. Forgetting to handle this can cause errors in filters, pagination, or other logic.
  • Treating query values as numbers without conversion: Query parameters are always received as strings. For example, limit=10 will come through as “10”. If you need numeric operations, explicitly convert the values.
  • Using query params for sensitive data: Query strings are visible in URLs, browser history, and logs. They should not be used to send confidential information such as passwords or tokens.
  • Not validating user input: Accepting query params without validation can lead to invalid values breaking the application or, worse, security vulnerabilities such as injection attacks.

Deprecated Methods

req.param(): Earlier versions of Express allowed fetching parameters using req.param(), which mixed query, route, and body parameters. This method has been deprecated in Express 4.x and above because it caused confusion about the source of values.

Modern alternatives

  • req.query → for query parameters
  • req.params → for route parameters
  • req.body → for request body data

Working with Multiple Query Parameters

In most real-world APIs, you will often need to handle more than one query parameter at a time. Query strings support this by chaining key–value pairs together with an ampersand (&).

For example:

/products?category=books&sort=asc&page=2&limit=10

In this request:

  • category=books → filter products by category
  • sort=asc → set the sort order
  • page=2 → specify which page of results to return
  • limit=10 → control how many items appear on a page

When Express processes this request, it automatically parses all query parameters and makes them available together as key–value pairs. This allows you to:

  • Apply multiple filters (e.g., category, brand, price range).
  • Combine pagination parameters like page and limit.
  • Support advanced functionality such as sorting, searching, and feature flags.

Handling multiple query parameters is no different from handling a single one, Express collects them all into the same object. What changes is how you organize, validate, and use them to drive your application logic.

Working with Missing or Optional Parameters

Not every client request will include all possible query parameters. Some may be optional, while others may be missing entirely. By default, when a query parameter is not provided in the URL, Express assigns it the value undefined.

For example, a request to:

/search?q=node

Might only include a search term without pagination or sorting options. In this case, parameters like page or limit would not exist.

To handle this gracefully:

  • Set default values: Define sensible defaults (for example, page=1 and limit=10) so the application continues to work even if these parameters are not supplied.
  • Treat parameters as optional: Check if a parameter exists before using it in filtering or calculations. If it’s not present, your logic should still return a valid response.
  • Avoid errors with conditional logic: Failing to account for missing parameters can cause runtime issues, especially when performing operations that assume a number or a string is always present.
  • Communicate clearly through API docs: Clearly mark which query parameters are required and which are optional so consumers of your API know what to expect.

By treating some query parameters as optional and supplying defaults where necessary, you make your API more robust and user-friendly.

Debugging Query Params with Requestly HTTP Interceptor

During development and testing, it’s common to experiment with different query parameter values to check how an API behaves. Manually editing URLs or constantly modifying code can slow down this process. This is where the Requestly HTTP Interceptor becomes useful.

Requestly by BrowserStack allows you to intercept outgoing requests from the browser or client and modify them on the fly. With it, you can:

  • Rewrite query parameters instantly: Change /users?page=1 to /users?page=50 without touching the code.
  • Simulate missing or invalid values: Test how your API handles limit=-1 or role= to confirm that validation and error handling are working.
  • Test edge cases quickly: Add or remove query parameters in real time to verify default behaviors.
  • Debug live environments safely: Try different inputs against staging or production APIs without redeploying code.

This flexibility helps developers and testers save time when verifying how Express routes respond to query parameters under different scenarios. Instead of relying solely on test cases or manual edits, Requestly provides an interactive way to explore and debug query behavior.

Try Requestly for Free

Conclusion

Query parameters make Express.js routes more flexible by enabling features like filtering, sorting, and pagination. Handling them correctly, by setting defaults, validating inputs, and avoiding deprecated methods, ensures your APIs remain reliable and secure.

For faster debugging, tools like Requestly HTTP Interceptor let you modify query parameters in real time and test different scenarios without changing your code, saving time during development.

Tags
Automation Testing UI Testing Website Testing

Get answers on our Discord Community

Join our Discord community to connect with others! Get your questions answered and stay informed.

Join Discord Community
Discord