How to get GET (query string) variables in Express.js on Node.js?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
In Express.js, query string parameters from GET requests are available on req.query, a parsed object where each key maps to its value from the URL. For a request to /search?q=nodejs&page=2, req.query.q returns "nodejs" and req.query.page returns "2". Express parses the query string automatically using the qs library, so no additional middleware is needed.
Basic Usage
All req.query values are strings. If the URL has no query string, req.query is an empty object {}.
Accessing Multiple Parameters
Use destructuring for clean access to multiple parameters.
Type Conversion
Query string values are always strings. Convert them explicitly:
Default Values
Arrays in Query Strings
Express supports arrays via repeated keys or bracket notation:
When only one value is provided, req.query.tags is a string, not an array. Normalize it:
Nested Objects
Express uses the qs library by default, which supports nested objects:
req.query vs req.params vs req.body
Validation with express-validator
URL Encoding
Special characters in query strings must be URL-encoded:
Common Pitfalls
- Values are always strings:
req.query.pageis"2", not2. UseparseInt()orparseFloat()for numbers, and=== 'true'for booleans. Forgetting this causes bugs in comparisons and arithmetic. - Missing parameters are
undefined:req.query.missingreturnsundefined, notnullor empty string. Use|| defaultor optional chainingreq.query.key?.trim(). - Single value vs array inconsistency:
?tag=onegives a string,?tag=one&tag=twogives an array. Always normalize with[].concat(req.query.tag || []). - Query string size limits: Express defaults to a max query string length of 1000 characters. Override with
app.set('query parser', qs => qs.parse(qs, { parameterLimit: 5000 }))if needed. - Prototype pollution: The
qsparser protects against__proto__injection by default in modern Express versions, but always validate and sanitize query parameters before using them in database queries.
Summary
- Use
req.queryto access GET query string parameters. No middleware needed - All values are strings; convert explicitly with
parseInt(),parseFloat(), or boolean comparison - Provide default values with
|| defaultfor optional parameters - Arrays are supported via repeated keys (
?a=1&a=2) or bracket notation (?a[]=1&a[]=2) - Use
req.paramsfor URL path segments,req.queryfor query strings,req.bodyfor POST data - Validate query parameters with
express-validatorfor production APIs
Related reading
- How to Get Signed S3 Url in AWS-SDK JS Version 3?
- How to get the browser to navigate to URL in JavaScript
- How to get the browser viewport dimensions?
- How to get the difference between two arrays in JavaScript?
- How to get the scroll bar with CSS overflow on iOS
- How to Get the Title of a HTML Page Displayed in UIWebView?
- How to handle errors from setTimeout in JavaScript?
- How to handle promise when calling async JS method from C using Emscripten
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.