title: How do I make an Express routing wildcard not catch 404'd static files?
body: <p>I'm dealing with a recurring problem in my Express app right now, which is that if a static file is requested which doesn't exist, my app will return my default index as that file. </p>
  
  <p>The short version of my routing in app.js is such:</p>
  
  <pre><code>const index = require('./routes/index');
  
  app.use(express.static(path.join(__dirname, 'public')));
  app.use('/*', index);
  app.use((req, res, next) =&gt; {
    console.log("DIDN'T FIND A FILE");
    const err = new Error('Not Found');
    err.status = 404;
    next(err);
  });
  </code></pre>
  
  <p>The problem is that I never get the error or the print, because the wildcard will always serve my index router. </p>
  
  <p>The server reports 200 for the request, and on the client I get the missing file whose contents is index. </p>
  
  <p>I want the wildcard so that I can redirect bad urls to the main page. Is the only solution here to just run regex on the request and guess if it's supposed to be a file and then manually return a 404? </p>