0% found this document useful (0 votes)
6 views1 page

Understanding Query Parameters in Express

Query parameters are additional information in a URL, formatted as key=value and accessed in Express.js using req.query. They are commonly used for search, filtering, sorting, and pagination in web applications. The document also includes assignment questions for creating various Express routes with specific query parameters.

Uploaded by

nawav81331
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views1 page

Understanding Query Parameters in Express

Query parameters are additional information in a URL, formatted as key=value and accessed in Express.js using req.query. They are commonly used for search, filtering, sorting, and pagination in web applications. The document also includes assignment questions for creating various Express routes with specific query parameters.

Uploaded by

nawav81331
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

■ 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.

You might also like