đ Introduction
Query parameters are key-value pairs appended to the end of a URL after a question mark (?). They allow clients to send additional information to the server without changing the URL path. In Express.js, query parameters are easily accessed using the req.query object.
Information
đ Prerequisites
- Node.js installed.
- Express.js installed.
- A basic Express server created.
- Basic understanding of HTTP requests and URLs.
â What Are Query Parameters?
Query parameters appear after the URL path and consist of one or more key-value pairs. Multiple parameters are separated using the & character.
| URL | Query Parameters |
|---|---|
| /search?keyword=laptop | keyword = laptop |
| /users?page=2 | page = 2 |
| /products?category=electronics&sort=price | category = electronics, sort = price |
đ URL Structure
đ Accessing Query Parameters
Express automatically parses query parameters and stores them in the req.query object.
Accessing Query Parameters
const express = require('express');
const app = express();
app.get('/search', (req, res) => {
const keyword = req.query.keyword;
res.send(`Searching for: ${keyword}`);
});
app.listen(3000);Visiting http://localhost:3000/search?keyword=laptop produces the following response:
Response
Searching for: laptopđ Multiple Query Parameters
Reading Multiple Parameters
app.get('/products', (req, res) => {
const category = req.query.category;
const sort = req.query.sort;
const page = req.query.page;
res.json({
category,
sort,
page
});
});Example request:
/products?category=electronics&sort=price&page=2
đ¤ Example Response
JSON Response
{
"category": "electronics",
"sort": "price",
"page": "2"
}Tip
đĸ Converting Query Parameters
Convert to Number
app.get('/users', (req, res) => {
const page = Number(req.query.page) || 1;
res.send(`Current Page: ${page}`);
});đ Providing Default Values
Default Query Parameter
app.get('/search', (req, res) => {
const keyword = req.query.keyword || 'all';
res.send(`Searching for: ${keyword}`);
});If the client visits /search without providing a keyword, the response will be:
Response
Searching for: allđ Common Use Cases
| Use Case | Example URL |
|---|---|
| Search | /search?keyword=phone |
| Pagination | /users?page=3 |
| Sorting | /products?sort=price |
| Filtering | /products?category=laptops |
| Language Selection | /docs?lang=en |
đ Request Lifecycle with Query Parameters
đ Query Parameters vs Route Parameters
| Feature | Query Parameters | Route Parameters |
|---|---|---|
| Location | After ? in the URL | Part of the URL path |
| Access Method | req.query | req.params |
| Purpose | Filtering, searching, sorting, pagination | Identifying a specific resource |
| Example | /products?category=books | /products/101 |
đĄ Best Practices
- Use query parameters for optional request data.
- Use route parameters to identify specific resources.
- Validate and sanitize query parameter values before using them.
- Provide sensible default values for optional parameters.
- Convert numeric query parameters using Number() or parseInt() when appropriate.
- Use descriptive parameter names to improve API readability.
Warning
đ Learn More
Explore the official Express.js documentation for handling requests:
âĸ Express.js Request - req.query
âĸ Express.js Official Documentation