How to get GET (query string) variables in Express.js on Node.js?


Can we get the variables in the query string in Node.js just like we get them in $_GET in PHP?

I know that in Node.js we can get the URL in the request. Is there a method to get the query string parameters?

5/4/2015 12:01:05 PM

Accepted Answer

In Express it's already done for you and you can simply use req.query for that:

var id =; // $_GET["id"]

Otherwise, in NodeJS, you can access req.url and the builtin url module to [url.parse] ( it manually:

var url = require('url');
var url_parts = url.parse(request.url, true);
var query = url_parts.query;
1/26/2019 6:10:37 PM

In Express, use req.query.

req.params only gets the route parameters, not the query string parameters. See the express or sails documentation:

(req.params) Checks route params, ex: /user/:id

(req.query) Checks query string params, ex: ?id=12 Checks urlencoded body params

(req.body), ex: id=12 To utilize urlencoded request bodies, req.body should be an object. This can be done by using the _express.bodyParser middleware.

That said, most of the time, you want to get the value of a parameter irrespective of its source. In that case, use req.param('foo').

The value of the parameter will be returned whether the variable was in the route parameters, query string, or the encoded request body.

Side note- if you're aiming to get the intersection of all three types of request parameters (similar to PHP's $_REQUEST), you just need to merge the parameters together-- here's how I set it up in Sails. Keep in mind that the path/route parameters object (req.params) has array properties, so order matters (although this may change in Express 4)


For Express.js you want to do req.params:

app.get('/user/:id', function(req, res) {
  res.send('user' +;    

I learned from the other answers and decided to use this code throughout my site:

var query = require('url').parse(req.url,true).query;

Then you can just call

var id =;
var option = query.option;

where the URL for get should be


//get query&params in express


app.get('/user/:id', function(req, res) {

  const query = req.query;// query = {sex:"female"}

  const params = req.params; //params = {id:"000000"}


If you are using ES6 and Express, try this destructuring approach:

const {id, since, fields, anotherField} = request.query;

In context:

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

app.get('/', function(req, res){
   const {id, since, fields, anotherField} = req.query;


You can use default values with destructuring too:

// sample request for testing
const req = {
  query: {
    id: '123',
    fields: ['a', 'b', 'c']

const {
  since = new Date().toString(),
  fields = ['x'],
  anotherField = 'default'
} = req.query;

console.log(id, since, fields, anotherField)