Reputation: 15
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?
Upvotes: 0
Views: 494
Reputation: 2068
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.
Upvotes: 1