0

This is for a library, and I cannot necessarily make this call:

app.use('/node_modules', express.static('node_modules'));

I have to do this a different way. Because I have no control over how the server serves static assets. Say I have this structure:

project/
  node_modules/
         xxx/
           asset.js
           index.html 
  app.js

and in app.js I have:

const express = require('express');
const app = express();

app.use('/docs', (req,res){
   res.render(require.resolve('./node_modules/xxx/index.html'));
});

app.listen(3000);

the problem is since the server is running from the current root directory, the index.html file won't have the right path to pick up the assets.js file.

For example, in index.html if base is set like so:

<base href="/">

that won't work. I could try using:

<base href="node_modules/xxx/">

Another solution would be to bundle everything together so that the index.html file doesn't need to make any requests for static assets, but I am looking for a good solution for how make sure the path resolution works somewhat generically. For example, if the express server is not launched in the project root, but another subdir.

  • 1
    You refer to "pick up assets.js" in your question, but you don't show us how you're trying to access that. Please show us the HTML that is trying to load assets.js. You should probably NOT be using relative paths for core assets. I can't really tell if this is what you need or not: [How to include scripts located inside the node_modules folder?](https://stackoverflow.com/questions/27464168/how-to-include-scripts-located-inside-the-node-modules-folder/27464258#27464258). – jfriend00 Dec 08 '18 at 06:20
  • Remember, Express does not serve ANY files by default. So, if you want your express server to serve assets.js when the browser requests it, you need a route in your Express server that sends that file. And, your HTML should probably specify an absolute path, not a relative path. – jfriend00 Dec 08 '18 at 06:20
  • This question is cross posted here: https://softwareengineering.stackexchange.com/questions/382682/correct-way-to-serve-file-thats-in-node-modules – jfriend00 Dec 08 '18 at 06:22
  • @jfriend00 not a dupe, I guess it wasn't clear enough, I updated the OP, I cannot make the `express.static` call that normally could be done. –  Dec 08 '18 at 10:18
  • Well, then you can start by answering the questions I put in my first comment. And, you can explain why you can't use `express.static()`. Your question is not very complete. Show us the HTML. We can't understand what the browser is requesting (and therefore what you need a route for) if you don't show us the actual HTML in question here. The solution is easy once we have enough data to understand the problem. – jfriend00 Dec 08 '18 at 16:23
  • 1
    I added more info to the duplicate answer in case it now covers your situation. Please check out the update. – jfriend00 Dec 08 '18 at 17:21

1 Answers1

1

Suppose your file is in the node_modules with path like './node_modules/bootstrap/dist/'

You can create a static route like '/file'

You can use:

app.use('/file',express.static(__dirname+'/node_modules/bootstrap/dist/'));

Include script like this:

<script src="/file/bootstrap.min.js"></script>
Faiz Khan
  • 288
  • 1
  • 9