node / express REST API application - passing parameters to GET method
<p><strong>Background</strong></p>

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

<p>In the file I have the following code: </p>

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

<p>When I start the application and navigate to <a href="http://myserver/tutorial1/emergency" rel="noreferrer">http://myserver/tutorial1/emergency</a>
everything works fine and I see the "respond with a resource" message. </p>

<p><strong>Goal</strong></p>

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

<pre><code> http://myserver/tutorial1/emergency
</code></pre>

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

<pre><code> http://myserver/tutorial1/emergency/12345
</code></pre>

<p>and the system should query the database for emergency record 12345 and return the appropriate result set. </p>

<p><strong>Problem / Question</strong></p>

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

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

<p>Now when I run the application, and browse to   </p>

<pre><code> http://myserver/tutorial1/emergency/12345
</code></pre>

<p>it works.  However, browsing to </p>

<pre><code>  http://myserver/tutorial1/emergency 
</code></pre>

<p>fails with a 404 error message. </p>

<p>Not Found</p>

<p>404</p>

<pre><code>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)
</code></pre>

<p>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 : </p>

<pre><code> http://myserver/tutorial1/emergency
</code></pre>

<p>or should it always look like this: </p>

<pre><code> http://myserver/tutorial1/emergency/{id}
</code></pre>

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

<pre><code> http://myserver/tutorial1/emergency/all
</code></pre>

<p>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. </p>

<p><strong>EDIT 1</strong></p>

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

<pre><code>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');
});
</code></pre>

<p>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. </p>