Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 231
Happydevdays Avatar asked Sep 28 '16 14:09

Happydevdays


People also ask

How do I get the request body in Express?

Express has a built-in express. json() function that returns an Express middleware function that parses JSON HTTP request bodies into JavaScript objects. The json() middleware adds a body property to the Express request req . To access the parsed request body, use req.

What does Express () method do?

The application will then return a response to the web browser, often dynamically creating an HTML page for the browser to display by inserting the retrieved data into placeholders in an HTML template. Express provides methods to specify what function is called for a particular HTTP verb ( GET , POST , SET , etc.)


Video Answer


1 Answers

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);
});
like image 129
Tom Avatar answered Sep 20 '22 03:09

Tom