title: Express.js Handle unmached routes
body: <p>Fellows I develop a Rest API and I want when a route does not exist to send a custom message instead of an html one that express.js sends by default. As fas as I searched I could not find a way to do that.</p>
  <p>I tried to do:</p>
  <pre><code>  app.all(&quot;*&quot;,function(req,res){
       res.status(404)
       res.header(&quot;Content Type&quot;,&quot;application/json&quot;)
       res.end(JSON.stringify({message:&quot;Route not found&quot;}))
    });
  </code></pre>
  <p>But it matches and all already implemented methods. I want only the unmached one to get handled by my app.</p>
  <h1>Edit 1</h1>
  <p>For each enndpoint I create a seperate file having the following content: eg. <code>myendpoint.js</code></p>
  <pre><code>module.exports=function(express){
  
     var endpoint=&quot;/endpoint&quot;
  
     express.get(endpoint,function(req,res){
        res.end(&quot;Getting data other message&quot;)
     }).post(endpoint.function(req,res){
        res.end(&quot;Getting data other message&quot;)
     }).all(endpoint,function(req,res){
        res.status(501)
        res.end(&quot;You cannot &quot;+res.method+&quot; to &quot;+endpoint)
     })
  }
  </code></pre>
  <p>An in my main file I use:</p>
  <pre><code>var endpoint=require('myendpoint.js')
  var MyEndpointController=endpoint(app)
  app.all(&quot;*&quot;,function(req,res){
     res.status(404)
     res.header(&quot;Content Type&quot;,&quot;application/json&quot;)
     res.end(JSON.stringify({message:&quot;Route not found&quot;}))
  });
  </code></pre>