Get Query String Parameters In Express

How To Get Query String Parameters In Express Web App

In this tutorial, you’ll learn how to get query string parameters in express web app. I’ll start from the very scratch and create an express app with a GET route. We’ll make a GET request to the route and see how to get query string parameters.

Getting Started With Express Web App

Let’s get started by creating an express web app. Create a project directory and initialize the node project.

mkdir GetNode
cd GetNode
npm init

Once the project has been initialized, install express framework into the project.

// install express framework
npm install --save express

Create a file called app.js and add the following code:

const express = require('express')
const app = express()
const port = 3000

app.get('/', (req, res) => res.send('Welcome to Get Query String Parameters In Express!'))

app.listen(port, () => console.log(`App listening on port ${port}!`))

Save the above changes and run the express web app.

// run the app
node app.js

You will have the application running at http://localhost:3000.

Get Query String Parameters In Express

Let’s create a GET request route into which we’ll be passing some query string parameters.

app.get('/getParameter', (req, res) => {
    
})

You can get the query string parameters using the req object passed into the route. req.query contains all query string parameters which are passed into the route.

Let’s say you have a route request as shown:

// sample route request
http://localhost:3000/getParameter?message=hello_boy

To get the query string parameters from the route URL, modify the code as shown:

app.get('/getParameter', (req, res) => {
    res.json({
        'message':req.query.message
    })
})

Save the above changes and restart the web app. Point your browser to http://localhost:3000/getParameter?message=hello_boy and you will have the query string parameters returned as JSON.

{"message":"hello_boy"}

Wrapping It Up

In this tutorial, you saw how to get query string parameters in express web app. Hope this will useful for someone trying to get query string parameters from a route.

Do let us know your thoughts and suggestions in the comments below.