When you send a GET request from the frontend to the backend, sometimes the server needs additional data to process that request.
Why is this needed?
Imagine you're using an e-commerce website. When you try to buy or search for an item, the backend needs to know:
- Which item are you looking for? Without that information, the server can’t respond correctly.
Query Parameters
Query parameters allow you to send data (like names, IDs, or search terms) along with a GET request.
They are appended to the end of a URL and help the backend understand what exactly you want.
URL Examples
Without query parameters:
http://localhost:3000/search
With a single query parameter:
http://localhost:3000/search?q=powerbank
Understanding the Syntax
In the URL: http://localhost:3000/search?q=powerbank
?q=powerbank
- "?" → starts the query string
- "q" → parameter name (key)
- "=" → assigns value
- "powerbank" → parameter value
We are sending the parameter value powerbank which is assigned to the variable q.
Multiple Query Parameters
You can also send multiple values by separating them with &:
http://localhost:3000/search?q=phone&price=1000
Here:
-
q= "phone" -
price= "1000"
Accessing Query Parameters in Backend (Express.js)
You can easily access query parameters using req.query.
Example:
const express = require('express');
const app = express();
app.get('/search', (req, res) => {
const searchTerm = req.query.q;
const price = req.query.price;
res.send(
`Search: ${searchTerm || 'none'}, Price: ${price || 'not specified'}`
);
});
How it works
If you visit:
http://localhost:3000/search?q=powerbank
The server will respond with:
Search term: powerbank
Conclusion
Query parameters are a simple and powerful way to:
- Send data in GET requests
- Customize backend responses
- Build dynamic features like search, filtering, etc.
If you're building full-stack apps, understanding query parameters is essential. They’re used everywhere from search bars to filtering APIs.
Top comments (0)