0

I'm running a Node.js server along with an Angular frontend. One of the Angular dependencies I'm using requires me to import a javascript file into my html page, by the name of swing.js. However, when I try to do this, it sends the required file as an http request to the server, resulting in requests that look like the following:

http://localhost:3000/home/me/app/node_modules/angular-swing/dist/swing.js

Obviously, this comes up as a 404. As an alternative, I've tried changing

<script src="/home/me/app/node_modules/angular-swing/dist/swing.js"></script>

into

<script src="swing.js"></script>

and then on the server-side, doing:

app.get('swing.js', function(req, res){
    res.sendFile('home/me/app/node_modules/angular-swing/dist/swing.js');
});

This works a little more, but then the file doesn't run properly, as I'm assuming it's no longer in the npm environment it needs to be in. I've tried multiple iterations of changing

<script src="/home/me/app/node_modules/angular-swing/dist/swing.js"></script>

into something that uses periods (.) to represent more relative paths, but that doesn't appear to work either. Overall, I'm very stuck, and would appreciate any insight. If it's of any use, I'm also using:

app.use(express.static(__dirname+'/public'));

db2791
  • 1,040
  • 6
  • 17
  • 30
  • node.js does not serve any files by default so any script files that you need send from your server to the client upon request need an explicit route to handle them or they need some generic route that knows how to handle all the requested script files. `swing.js` in the client is not part of any "NPM environment". It's running the browser at that point, not in any NPM enviornment. It's possible that swing.js itself needs some other scripts or resources that also need routes and that's why it doesn't work after you make an explicit route for it. – jfriend00 Jul 24 '16 at 23:13
  • You may also want to read this: [How to include scripts located inside the node_modules folder?](http://stackoverflow.com/questions/27464168/how-to-include-scripts-located-inside-the-node-modules-folder/27464258#27464258). – jfriend00 Jul 24 '16 at 23:14
  • @jfriend00 Thanks for clearing that up. On top of eventually needing to clean up my routes, my problem was also the placement of the script, which came before one of its dependencies. – db2791 Jul 24 '16 at 23:28

1 Answers1

0

Making my comments into an answer...

node.js does not serve any files by default so any script files that you need sent from your server to the client upon request need an explicit route to handle them or they need some generic route that knows how to handle all the requested script files.

swing.js in the client is not part of any "NPM environment". It's running the browser at that point, not in any NPM enviornment. It's possible that swing.js itself needs some other scripts or resources that also need routes and that's why it doesn't work after you make an explicit route for it. You can probably examine the specific errors in the console to give you a clue why it isn't working.

You may also want to read this: How to include scripts located inside the node_modules folder?

Community
  • 1
  • 1
jfriend00
  • 683,504
  • 96
  • 985
  • 979