What is the query string in a URL and how do you access it in Express?

Why Interviewers Ask This

Interviewers use this question to quickly assess whether a candidate has the foundational knowledge required for Node.js development. It reveals whether you understand the building blocks that more complex concepts rely on.

Answer

A query string is the portion of a URL after the ? character, containing key-value pairs separated by &. Example: /search?q=nodejs&page=2&limit=10. In Express.js, query parameters are automatically parsed and available in req.query as an object: const { q, page, limit } = req.query; — values are always strings, so convert types as needed: const pageNum = parseInt(page, 10) || 1;. Route parameters (different from query params) are segments of the URL path prefixed with : — e.g., /users/:id — accessible via req.params.id. Request body (for POST/PUT) is accessible via req.body after using express.json() middleware. Always validate and sanitize query parameter values before using them in queries or logic, as they come from untrusted user input.

Common Mistake

A common mistake is memorizing definitions without understanding implications. When asked this question, go one level deeper — explain what happens when this concept is misused or ignored.