node / express REST API application - passing parameters to GET method

24,149

Solution 1

Do I need to create two separate methods, one that accepts a parameter and one that doesn't?

I'm guessing you posted this before even trying that?

The answer is yes.

Your route that accepts parameters should look like this:

app.get('/emergency/:id', function (req, res, next) {
    var id = req.params.id;
    console.log('The id: ' + id);
});

Solution 2

Yes,it is necessary to have two different functions that listen to each type of url. This is because The url

http://localhost/example/abcd will get received in 
router.get('/example/:username',testFunction1);

whereas

http://localhost/example will be received in 
router.get('/example',testFunction2);

Refer to the article: http://codewhoop.com/article/Nodejs%20Getting%20parameters%20through%20GET%20method for detailed explanation of procedure to fetch paramters through GET method in Nodejs.

Share:
24,149
Happydevdays
Author by

Happydevdays

Updated on July 09, 2022

Comments

  • Happydevdays
    Happydevdays almost 2 years

    Background

    I'm writing a sample nodejs / express REST interface or API for the purposes of learning. I've created a new route called "emergency".

    In the file I have the following code:

    router.get('/', function(req, res, next) {
            //var ip = req.params.ip;
            res.send('respond with a resource');
    });
    

    When I start the application and navigate to http://myserver/tutorial1/emergency everything works fine and I see the "respond with a resource" message.

    Goal

    I'd like my application to be able to accept parameters as well. So fro example, when a user navigates to

     http://myserver/tutorial1/emergency
    

    I want all emergency numbers to be queried and returned. But they should also be able to do this:

     http://myserver/tutorial1/emergency/12345
    

    and the system should query the database for emergency record 12345 and return the appropriate result set.

    Problem / Question

    In order to accommodate both types of GET queries, I've changed the code to look like this:

    router.get('/id', function(req, res, next) {
            //var ip = req.params.ip;
            res.send('respond with a resource');
    });
    

    Now when I run the application, and browse to

     http://myserver/tutorial1/emergency/12345
    

    it works. However, browsing to

      http://myserver/tutorial1/emergency 
    

    fails with a 404 error message.

    Not Found

    404

    Error: Not Found
        at /var/www/html/nodejs_samples/tutorial1/app.js:34:13
        at Layer.handle [as handle_request] (/var/www/html/nodejs_samples/tutorial1/node_modules/express/lib/router/layer.js:95:5)
        at trim_prefix (/var/www/html/nodejs_samples/tutorial1/node_modules/express/lib/router/index.js:312:13)
        at /var/www/html/nodejs_samples/tutorial1/node_modules/express/lib/router/index.js:280:7
        at Function.process_params (/var/www/html/nodejs_samples/tutorial1/node_modules/express/lib/router/index.js:330:12)
        at next (/var/www/html/nodejs_samples/tutorial1/node_modules/express/lib/router/index.js:271:10)
        at /var/www/html/nodejs_samples/tutorial1/node_modules/express/lib/router/index.js:618:15
        at next (/var/www/html/nodejs_samples/tutorial1/node_modules/express/lib/router/index.js:256:14)
        at Function.handle (/var/www/html/nodejs_samples/tutorial1/node_modules/express/lib/router/index.js:176:3)
        at router (/var/www/html/nodejs_samples/tutorial1/node_modules/express/lib/router/index.js:46:12)
    

    Do I need to create two separate methods, one that accepts a parameter and one that doesn't? (aka method overloads?) Perhaps my understanding of REST is what's faulty. Should a GET request look like :

     http://myserver/tutorial1/emergency
    

    or should it always look like this:

     http://myserver/tutorial1/emergency/{id}
    

    Maybe the proper way to do a GET for all records is something like this:

     http://myserver/tutorial1/emergency/all
    

    I'm trying to google my question right now as well, but I'm having a hard time expressing it succinctly enough to get an accurate search result set.

    EDIT 1

    This is what my code looks like when I try to create two methods (and this works)

    router.get('/', function(req, res, next) {
      res.send('respond with a resource');
    });
    
    router.get('/:id', function(req, res, next) {
            var id = req.params.id;
            console.log(id);
            res.send('got it');
    });
    

    But this just feels odd because I guess I'm used to other frameworks in other languages where the system can check for empty params so you just need one method. this is not a complaint! just a comment that might explain why my brain is "expecting" the system to work a different way.