I want to know how does EXPRESS parse multiple query parameters with the same name; I couldn't find any useful reference anywhere. I want to know specifically about EXPRESS, how is it going to treat this URL www.example.com/page?id=1&id=2&id=3.....id=n
Any word after the question mark (?) in a URL is considered to be a parameter which can hold values. The value for the corresponding parameter is given after the symbol "equals" (=). Multiple parameters can be passed through the URL by separating them with multiple "&".
Query parameters are passed after the URL string by appending a question mark followed by the parameter name , then equal to (“=”) sign and then the parameter value. Multiple parameters are separated by “&” symbol.
You can use the usual req.query. Whenever there's multiple query parameters with the same name, req.query[paramName]
will return an array instead of the value. So in your case:
app.get("/page", (req, res) => {
const { id } = req.query
console.log("ID is "+ id)
});
// GET www.example.com/page?id=1&id=2&id=3
// ID is ["1", "2", "3"]
// GET www.example.com/page?id=12345
// ID is 12345
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With