Difference between req.query and req.params in Express
Aspect |
||
Source of Data |
Extra bits at the end of a URL (e.g., form inputs, search bar) |
Changing parts in the URL marked by colons |
Example URL |
‘/search?q=example’ |
‘/users/:id’ |
Usage |
Ideal for handling URL parameters, especially with search terms |
Useful when dealing with dynamic values within a consistent URL structure |
Express.js Example |
‘javascript app.get(‘/search’, (req, res) => { const searchTerm = req.query.q; // Process search term }); |
`javascript app.get(‘/users/:id’, (req, res) => { const userId = req.params.id; // Fetch user details based on dynamic user ID }); |
Scenario Example |
Handling a search feature on a website |
Accessing user-specific information on a page |
Difference between req.query and req.params in Express
In this article, we will learn about the req.query and req.params and we will also learn the difference between them.
Table of Content
- req.query: Dealing with URL Stuff
- req.params: Figuring Out Route Things
- Difference between req.query and req.params in Express.js