title: 'missing routes' error handling in node.js express app (failing)
body: <p>I am simply trying to work within my <code>app.js</code> or router file to handle possible missing router errors. <em>(similar stackoverflow solutions have not worked for me)</em></p>
  
  <p>my <code>app.js</code> or router files is thus <em>(works fine as is; but am trying to handle missing router error properly)</em>:</p>
  
  <pre><code>'use strict'
  var path = require('path');
  var film = require('./healthymeals.js');
  var express = require('express');
  var router = express.Router();
  var app = express();
  
  module.exports = function(app) {
  
    app.get('/allMeals', film.getAllMeals);
  
    app.get('/meals/:id', film.getMealInfo);
  
    app.get('/meals/:id/options',meal.getHealthyMealoptions);
  
    app.get('*', function(req, res) {
      res.send("Healthy meals");
    });
  </code></pre>
  
  <p>I have installed npm's/express error handling package '<a href="https://www.npmjs.com/package/http-status-codes" rel="nofollow noreferrer"><strong>http-status-codes</strong></a>' .</p>
  
  <p>And <em>tried</em> it's implementation: </p>
  
  <pre><code>var HttpStatus = require('http-status-codes');
  
  response
      .status(HttpStatus.OK)
      .send('ok');
  
  response
      .status(HttpStatus.INTERNAL_SERVER_ERROR)
      .send({
          error: HttpStatus.getStatusText(HttpStatus.INTERNAL_SERVER_ERROR)
      });
  </code></pre>
  
  <p>when the above is ran; I get a <strong>build error</strong> in terminal thrown on the '<code>response</code>' word. <em>I had no errors when installing relevant npm package.</em> </p>
  
  <pre><code>ReferenceError: response is not defined
  </code></pre>
  
  <p>I have <em>then</em> tried a few suggestions I found on stackover, <em>ie.</em></p>
  
  <pre><code>function getAllApps(request, response) {
      appService.getApps(request.query.$expand).then(function (apps) {
          response.status(200).send(apps);
          })
      .catch(function (err) {
        console.error('Error occurred in Apps Api: ' + err);
        response.status(500).send("" + err);
      });
  }
  </code></pre>
  
  <p>which is not effective and seems to just get ignored. any pointers appreciated <em>(first time using this stack)</em>. cheers</p>
  
  <hr>
  
  <p><strong>Update,</strong> with the answer below from Jonas w - I was first getting time out error as follows:</p>
  
  <pre><code>Error: Timeout of 2000ms exceeded. For async tests and hooks, ensure "done()" is called; if returning a Promise, ensure it resolves.
  </code></pre>
  
  <p>And then after increasing time out, I am getting below error:</p>
  
  <pre><code>TypeError: Cannot read property 'status' of undefined
  </code></pre>
  
  <hr>
  
  <p>For more context here is the test in which it is <em>not</em> passing with my attempts:</p>
  
  <pre><code>  describe('error handling', function() {
          it('handles missing routes', function(done) {
              request
                  .get('/meals/brokenroute')
                  .expect(404)
                  .expect(function(res) {
                      ok('message' in res.body, '"message" key missing');
                  })
                  .end(done);
          });
  </code></pre>