Retrieve the GET query string parameters using Express
The query string is the part that comes after the URL path and starts with a question mark ('?'). Let's see how to get the properties and their values.
The query string is the part that comes after the URL path, and starts with a question mark ?
.
For example:
?name=flavio
Multiple query parameters can be added using &
:
?name=flavio&age=35
How do you get those query string values in Express?
Express makes it very easy by populating the Request.query
object for us:
const express = require('express')
const app = express()
app.get('/', (req, res) => {
console.log(req.query)
})
app.listen(8080)
This object is filled with a property for each query parameter.
If there are no query params, it’s an empty object.
This makes it easy to iterate on it using the for…in loop:
for (const key in req.query) {
console.log(key, req.query[key])
}
This will print the query property key and the value.
You can access single properties as well:
req.query.name //flavio
req.query.age //35
I wrote 21 books to help you become a better developer:
- HTML Handbook
- Next.js Pages Router Handbook
- Alpine.js Handbook
- HTMX Handbook
- TypeScript Handbook
- React Handbook
- SQL Handbook
- Git Cheat Sheet
- Laravel Handbook
- Express Handbook
- Swift Handbook
- Go Handbook
- PHP Handbook
- Python Handbook
- Linux Commands Handbook
- C Handbook
- JavaScript Handbook
- Svelte Handbook
- CSS Handbook
- Node.js Handbook
- Vue Handbook
Also, JOIN MY CODING BOOTCAMP, an amazing cohort course that will be a huge step up in your coding career - covering React, Next.js - next edition February 2025