ExpressJS URL Building & Route Parameters
Imagine you have an online store with thousands of products. You don't want to create a separate route for each product like
/products/1, /products/2, /products/3 and so on. That would be impossible! Instead, you need a way to handle dynamic values in URLs. This is where route parameters come in.What are Route Parameters?
Route parameters are named segments in the URL that capture the value at that position. They are defined with a colon (
:) followed by the parameter name. When a request matches that route, the captured values are available in req.params.Basic Route Parameter Example
app.get('/users/:userId', (req, res) => {
const userId = req.params.userId;
res.send(`You requested user with ID: ${userId}`);
});Now, when someone visits:
/users/42→ sends "You requested user with ID: 42"/users/john→ sends "You requested user with ID: john"/users/100→ sends "You requested user with ID: 100"
Multiple Route Parameters
You can have multiple parameters in a single route:
app.get('/products/:category/:productId', (req, res) => {
const category = req.params.category;
const productId = req.params.productId;
res.send(`Looking at product ${productId} in category ${category}`);
});For
/products/electronics/25, it would send: "Looking at product 25 in category electronics"Optional Parameters
You can make parameters optional by adding a question mark:
app.get('/users/:userId?', (req, res) => {
if (req.params.userId) {
res.send(`Viewing user ${req.params.userId}`);
} else {
res.send('Viewing all users');
}
});Now
/users and /users/5 both work, with different behavior.Query Parameters
Besides route parameters, URLs can also have query parameters (the part after ?). These are used for filtering, sorting, or optional data. Query parameters are available in
req.query.app.get('/products', (req, res) => {
const category = req.query.category;
const sort = req.query.sort;
res.send(`Filtering products by category: ${category}, sort: ${sort}`);
});If you visit
/products?category=electronics&sort=price, you'll see: "Filtering products by category: electronics, sort: price"Route Parameters vs Query Parameters
It's important to understand when to use each:
- Route parameters are for required, identifying parts of the URL (like user ID, product ID).
- Query parameters are for optional filters, sorting, pagination, or additional data.
Building URLs Programmatically
Sometimes you need to generate URLs in your code. You can use template literals:
const userId = 42;
const url = `/users/${userId}`; // /users/42For query strings, you can use the URLSearchParams API:
const params = new URLSearchParams({ category: 'books', sort: 'price' });
const url = `/products?${params.toString()}`; // /products?category=books&sort=priceTwo Minute Drill
- Route parameters (
:param) capture dynamic values from the URL path. - Access them via
req.params. - Query parameters (
?key=value) are for optional data, accessed viareq.query. - Use route parameters for required IDs, query parameters for filters and options.
- You can have multiple parameters and even make them optional.
Need more clarification?
Drop us an email at career@quipoinfotech.com
