1

I am having a node js server that serves index.html when user hits localhost:3000/

app.get('/', function (req, res){
  res.render('index.html');
});

However i cant figure out how i can refer to myscript.js javascript file from within the index.html.

<script src="script/my_script.js" type="text/javascript"></script>

I am calling a click button function from this file, but at run time this call gives 404. Please let me know how i can refer to a javascript file from within index.html in a nodejs server setup.

ttarik
  • 3,824
  • 1
  • 32
  • 51
trial999
  • 1,646
  • 6
  • 21
  • 36
  • possible duplicate of [Express-js can't GET my static files, why?](http://stackoverflow.com/questions/5924072/express-js-cant-get-my-static-files-why) – Naeem Shaikh Mar 24 '15 at 13:03

1 Answers1

4

You can set up a static directory in which express will serve files as-is. For example:

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

You can then create a "public" directory in your app root, and move your "script" folder inside. Any files (such as javascript, css) inside will be served directly to clients.

Docs

ttarik
  • 3,824
  • 1
  • 32
  • 51