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.

1

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.