?name=John&age=30. What will req.query contain?app.get('/user', (req, res) => {
res.json(req.query);
});Express uses the querystring module by default, which parses query parameters into an object with all values as strings.
?user[name]=Alice&user[age]=25 into a nested object. Which Express setup achieves this?The express.urlencoded({ extended: true }) middleware uses the qs library which supports nested objects in query strings.
req.query not parse arrays as expected??colors=red&colors=blue, req.query.colors is a string instead of an array. What causes this behavior?app.get('/colors', (req, res) => {
res.json(req.query.colors);
});Express's default query parser returns the last value for repeated keys as a string. To get arrays, the query string must use bracket notation or a custom parser.
app.set('query parser', 'simple') or app.set('query parser', 'extended'), how does it affect req.query?The 'simple' parser uses Node's querystring module which does not support nested objects. The 'extended' parser uses the qs library which supports nested objects and arrays.
/search?filters[price][min]=10&filters[price][max]=100&tags=red&tags=blue, what is the JSON response?app.use(express.urlencoded({ extended: true }));
app.get('/search', (req, res) => {
res.json(req.query);
});The extended: true option uses the qs library which parses nested objects and repeated keys into arrays. All values are strings.
