Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple query parameters with same name

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

like image 798
hakiki_makato Avatar asked Jun 26 '20 06:06

hakiki_makato


People also ask

How do you add multiple query parameters to a URL?

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 "&".

How do I pass multiple query parameters in REST URL?

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.


1 Answers

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
like image 78
akmalzamri Avatar answered Oct 02 '22 19:10

akmalzamri