title: Express serve static files inside Router
body: <p>I am currently working on a <code>nodejs</code> project, and I have a simple question. I want to serve some libraries from my <code>node_modules</code> folder statically to the client (<em>maybe stupid, but not relevant to the question</em>), but I dont want to trash my main server JS file with all these statically served files like this:</p>
  <pre class="lang-js prettyprint-override"><code>const express = require('express');
  const app = express();
  
  // Imports here
  app.use('/assets/lib/bootstrap', './node_modules/bootstrap/dist');
  app.use('/assets/lib/axios', './node_modules/axios/dist');
  app.use('/assets/lib/aos', './node_modules/aos/dist');
  
  app.listen(3000, () =&gt; {
    console.log('Server listening on port 3000');
  });
  </code></pre>
  <p>If I have 10+ imports, this would trash up my Server JS file, which I like to keep as clean as possible. I was wondering why this option wouldn't work:</p>
  <p><code>./routes/route1.js</code> <strong>:</strong></p>
  <pre class="lang-js prettyprint-override"><code>const express = require('express');
  const router = express.Router();
  const path = require('path');
  
  // Imports here
  router.use('/bootstrap', path.resolve(__dirname, 'node_modules/aos/dist'));
  router.use('/axios', path.resolve(__dirname, 'node_modules/aos/dist'));
  router.use('/aos', path.resolve(__dirname, 'node_modules/aos/dist'));
  
  // path to node_modules file is not the problem here
  
  module.exports = router
  </code></pre>
  <p>And in my main Server JS file:</p>
  <pre class="lang-js prettyprint-override"><code>const express = require('express');
  const app = express();
  const route1 = require('./routes/route1');
  
  // Imports here
  app.use('/assets/lib', route1); 
  
  app.listen(3000, () =&gt; {
    console.log('Server listening on port 3000');
  });
  </code></pre>
  <p>But this gives me an error. The file is not statically served to the client. Is this because an express Router can't serve static files? Maybe this is a rookie mistake, but any help is appreciated. The first code snippet does work, but I'm trying to keep my code organized.</p>