by Sai gowtham

How to access the GET query parameters in Express

Learn, how to access the query parameters from a url in express.

Query parameters are passed at the end of a url using a question mark(?) followed by the key-value pairs.

Example:

localhost:3000/users?name=gowtham

In this url, the key is name and value is gowtham

Multiple query parameters can be passed by using a & (And operator).

localhost:3000/users?name=gowtham&id=1234

Accessing query parameters

To access the query parameters in an express route handler, we need to use req.query object.

const express = require('express');
const app = express();

app.get('/users', (req, res) => {
    const name = req.query.name;    const id = req.query.id;    res.send(`User is ${name}, id is ${id}`);
});

app.listen(3000, () => console.log(`App is running`));

Top Udemy Courses

JavaScript - The Complete Guide 2020 (Beginner + Advanced)
JavaScript - The Complete Guide 2020 (Beginner + Advanced)
26,545 students enrolled
52 hours of video content
View Course
React - The Complete Guide (incl Hooks, React Router, Redux)
React - The Complete Guide (incl Hooks, React Router, Redux)
221,520 students enrolled
44 hours of video content
View Course
Vue JS 2 - The Complete Guide (incl. Vue Router & Vuex)
Vue JS 2 - The Complete Guide (incl. Vue Router & Vuex)
114,575 students enrolled
21 hours of video content
View Course