Performance: Query string parsing
This affects server response time and initial page load speed by how quickly query strings are parsed and processed.
app.get('/search', (req, res) => {
const query = req.query;
// process query
res.send(query);
});app.get('/search', (req, res) => { const query = require('querystring').parse(req.url.split('?')[1]); // process query res.send(query); });
| Pattern | CPU Cost | Event Loop Blocking | Response Time Impact | Verdict |
|---|---|---|---|---|
| Manual parsing with querystring.parse | High | Yes, blocks event loop | Increases response time by ~5-10ms | [X] Bad |
| Express built-in req.query | Low | No | Minimal impact on response time | [OK] Good |
/search?term=books?req.params for route params, req.body for POST data, and req.query for query strings.? in a URL. Express parses it into req.query as an object.page in an Express route handler?req.query as key-value pairs.page parameter, use req.query.page. Other options access wrong objects.app.get('/items', (req, res) => {
const limit = Number(req.query.limit) || 10;
res.send(`Limit is ${limit}`);
});/items?limit=5?Number(req.query.limit) to convert the string '5' to number 5.app.get('/search', (req, res) => {
const term = req.query.term.toLowerCase();
res.send(term);
});/search (no query parameter)?term is missing, req.query.term is undefined.toLowerCase() on undefined throws a TypeError.sort that can be either asc or desc. If missing or invalid, default to asc. Which code snippet correctly implements this in Express?req.query.sort is either 'asc' or 'desc'. Using includes() on an array is a clean way.Pass the test and this pattern moves unknown