Express + Node Route With Multiple Parameters in Query String

17,988

Solution 1

To use this URL in a route with Express:

 /api?paramA=valueA&paramB=valueB

You do this:

router.get('/api', function(req, res) {
    console.log(req.query.paramA);     // valueA
    console.log(req.query.paramB);     // valueB
    console.log(req.query.paramC);     // undefined (doesn't exist)
});

Query parameters are parsed into the req.query object. If the query parameter name does not exist in the query string, then that property will not exist on the query.query object and trying to read it will return undefined. Keep in mind that all values will be strings. If you desire them to be a number or some other data type, then you have to parse them into that other type.

Solution 2

I ended up using this. This code did exactly what I set out to do in the original question.

router.get('/api', function(req, res) {
    if(typeof req.query.paramA !== 'undefined' && typeof req.query.paramB !== 'undefined') {
    let paramA = req.query.paramA,   
        paramB = req.query.paramB;
    //do something with paramA and paramB
   }
});

Solution 3

It`s easier to define url parameters in router .

Example url : http://www.example.com/api/users/3&0

router.get('/api/users/:id&:pending', function (req, res) {
  console.log(req.params.id);
  console.log(req.params.pending);
});

Solution 4

If you are certain about the GET parameters that are being passed, so you could easily do it like this:

router.get('/api', function(req, res) {
    if(typeof req.params.paramA !== 'undefined' && typeof req.params.paramB !== 'undefined') {
    let paramA = req.params.paramA,   
        paramB = req.params.paramB;
    //do something with paramA and paramB
   }
});
Share:
17,988

Related videos on Youtube

MadPhysicist
Author by

MadPhysicist

A technologist, a musician, an artist and a scientist.

Updated on June 04, 2022

Comments

  • MadPhysicist
    MadPhysicist almost 2 years

    I am building an API in Node and am struggling to figure something out. Namely, I know how to build routes of the type /api/:paramA/:paramB. In this case, there are two parameters.

    The code would be something like this:

    router.get('/test/:paramA/:paramB', function(req, res) {
        res.json({ message: 'hooray! welcome to our api!' + req.params.paramA + req.params.paramB});   
    });
    

    How could one build a route that would respond at something like /api?paramA=valueA&paramB=valueB?

  • MadPhysicist
    MadPhysicist almost 7 years
    What if I need to check whether both of the parameters are present?
  • MadPhysicist
    MadPhysicist almost 7 years
    That is, is this still a solid solution for that case?
  • MadPhysicist
    MadPhysicist almost 7 years
    I had to do the following to get it working: let paramA = req.param('paramA'), paramB = req.param('paramB');
  • Gonras Karols
    Gonras Karols almost 7 years
    Quoted from Express 4.x API - "req.param(name [, defaultValue]) is Deprecated. Use either req.params, req.body or req.query, as applicable. Direct access to req.body, req.params, and req.query should be favoured for clarity - unless you truly accept input from each object.."
  • MadPhysicist
    MadPhysicist almost 7 years
    I am not sure why that happens but your version returns empty variables for me. I believe i have the latest stable Express installed.
  • jfriend00
    jfriend00 almost 7 years
    This answer seems to be missing :paramA and :paramB from the route definition.
  • jfriend00
    jfriend00 almost 7 years
    I would think it should be just like the OP had it: router.get('/test/:paramA/:paramB', function(req, res) {...}. If :paramA and :paramB are not in the route definition, then req.params.paramA and req.params.paramB will not be present. It is the route definition that causes them to get parsed and put into the req.params object.
  • MadPhysicist
    MadPhysicist almost 7 years
    @jfriend00 Yes, but I was looking to get the route of the following form: /api?paramA=valueA&paramB=valueB
  • MadPhysicist
    MadPhysicist almost 7 years
    @jfriend00 My question asks specifically for that. Read the very last sentence :)
  • jfriend00
    jfriend00 almost 7 years
    This is not the recommend way to do this. req.query is the proper place to access parsed parameters from the query string.
  • MadPhysicist
    MadPhysicist almost 7 years
    Yes, of course. Thanks for pointing this out! I copied the code wrong.