12

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.

3 Answers 3

19

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);
});
Sign up to request clarification or add additional context in comments.

6 Comments

no i have tried it. but because i'm a node js noob, i'm questioning whether my approach is correct. and I was also missing the ":" that you have here. If I have a routes/emergency.js file where all of this logic is in, do i actually need the 'emergency' in '/emergency/:id' that you have in your answer? I don't think I do
The code in your edit is fine. I don't understand your second question in your comment. The two route approach is fine, its simple and it works.
Tom, please see "Edit 1" section in my original question
Not sure what else you want - Express isn't about the change how they approach routing because your brain expects the system to work differently.
lol. Tom, i realize that. I think you're missing the point of my question. Because I'm inexperienced with the tool, I'm asking for validation of my current understanding of how i should use it.
|
1

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.

Comments

0

I am not sure will it work or not you can try some thing like this

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

1 Comment

thanks for the suggestion! I gave it a try and it works when I just try without a parameter. But when I pass a parm, it fails with a 404. If express / node isn't designed to work this way... it's ok. I'm just trying to understand what the "right" way to design my application is.

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.