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

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.