■ Query Parameters in Express.
js
1. What are Query Parameters?
Query parameters are extra information sent with a URL. They always start after a '?' in the URL
and follow the format: key=value. If multiple query parameters exist, they are separated by '&'.
Example: [Link] Here: q=express, page=2
2. How to Access Query Parameters in Express
In Express, query parameters can be accessed using [Link]. Example: [Link]('/search', (req,
res) => { const { q, category } = [Link]; [Link](`Search query: ${q}, Category: ${category ||
'none'}`); });
3. Real-Life Use Cases
- E-commerce: [Link]/search?q=shoes&brand;=nike&color;=black - Movies:
[Link]/search?genre=action&year;=2023 - News:
[Link]/articles?category=technology&page;=3
4. Easy Analogy ■
Think of going to a restaurant and ordering: 'I want a pizza (main route), but make it extra cheese
and no onions.' Here: Route = pizza, Query = cheese=extra, onion=no
■ Summary
- Query Parameters = extra instructions in a URL (after ?) - Format: key=value, multiple separated
by & - Accessed in Express using [Link] - Commonly used for: search, filter, sort, pagination,
and options
■ Assignment Questions
1 Q1. Create an Express route /products with query parameters category, brand, and price.
2 Q2. Build a /movies route with query parameters genre and year.
3 Q3. Create a /search route with query parameters q and page (default page=1).
4 Q4. Design a /news route with query parameters category and sort (asc/desc).
5 Q5. (Challenge ■) Make a /weather route with query parameters city and unit (C/F), default to
Celsius.