title: How to handle unknown pages in Node.js?
body: <p>Here is a question I have concerning a <code>Node.js</code> app of mine (working with <code>Express</code>) on <code>Heroku</code>.
  I want to handle unknown URL. More precisely:</p>
  
  <p>If I look at this URL in a browser: <a href="https://myapp.herokuapp.com/" rel="nofollow noreferrer">https://myapp.herokuapp.com/</a> I see what I expect.
  If I look a this one: <a href="https://myapp.herokuapp.com/xxyyyzzWhatever" rel="nofollow noreferrer">https://myapp.herokuapp.com/xxyyyzzWhatever</a> I also see what I expect. That is:</p>
  
  <pre><code>Cannot GET /xxyyyzzWhatever
  </code></pre>
  
  <p>Instead of displaying: <strong>Cannot GET /xxyyyzzWhatever</strong>
  I want to do other things. At this point I have referred to these documents:
  <a href="http://expressjs.com/en/guide/error-handling.html" rel="nofollow noreferrer">http://expressjs.com/en/guide/error-handling.html</a> and <a href="http://thecodebarbarian.com/80-20-guide-to-express-error-handling" rel="nofollow noreferrer">http://thecodebarbarian.com/80-20-guide-to-express-error-handling</a>; and I can get quite a bit of control on what is displayed.</p>
  
  <p>I have implemented this kind of code:</p>
  
  <pre><code>app.get('*', function(req, res, next) {
    setImmediate(() =&gt; { next(new Error('woops')); });
  });
  
  app.use(function(error, req, res, next) {
    const xPt = req.path.substring(1,req.path.length);
    res.json({ message: error.message reachPoint: xPt});
  });
  </code></pre>
  
  <p>But what I really want is to have something like:</p>
  
  <pre><code>if (xPt.substring(0,1) == "A") {display("You are lucky");}
  else if (xPt.substring(0,1) == "Z") {display("You are very unlucky");}
  else if ((xPt.substring(0,1) &gt;= "B")&amp;&amp;(xPt.substring(0,1) &lt;= "R")) 
    {goto("https://stackoverflow.com/")}
  else {goto("http://www.google.com/")}
  </code></pre>
  
  <p>Any tip on the way to go?</p>