0

I have a index.html file, which i want to send, using

app.get('/', (req, res) => { res.sendFile(`${__dirname}/index.html`) });

Also, I have some js files in this directory, which I want to use in HTML by

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

So, how can I make these js files accessible in HTML?

1

1 Answer 1

1

You can use what @fedeteka suggested.

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

The example above basically makes any file in the js folder to be accessed by the /js path. For example, if you have your file tree set up like

Main Folder
-----js
----------script.js
----------another.js
----------asmanyasyouwant.js

These files can be accessed by going to localhost:{yourport}/js/script.js or localhost:{yourport}/js/another.js, etc.

If you set it up this way, you can have

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

as long as you have a file called index.js in the js folder.

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.