5

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?

4 Answers 4

17

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.

Sign up to request clarification or add additional context in comments.

Comments

5

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
   }
});

2 Comments

This is not the recommend way to do this. req.query is the proper place to access parsed parameters from the query string.
Yes, of course. Thanks for pointing this out! I copied the code wrong.
4

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);
});

Comments

1

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
   }
});

9 Comments

What if I need to check whether both of the parameters are present?
That is, is this still a solid solution for that case?
I had to do the following to get it working: let paramA = req.param('paramA'), paramB = req.param('paramB');
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.."
I am not sure why that happens but your version returns empty variables for me. I believe i have the latest stable Express installed.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.