我有一个要发送的index.html文件,使用
app.get('/', (req, res) => {
res.sendFile(`${__dirname}/index.html`)
});
另外,我在此目录中有一些js文件,我想通过
<script src="./js/index.js"></script>
那么,如何使这些js文件可通过HTML访问?
我有一个要发送的index.html文件,使用
app.get('/', (req, res) => {
res.sendFile(`${__dirname}/index.html`)
});
另外,我在此目录中有一些js文件,我想通过
<script src="./js/index.js"></script>
那么,如何使这些js文件可通过HTML访问?
您可以使用@fedeteka的建议。
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 likeThese files can be accessed by going to
localhost:{yourport}/js/script.js
orlocalhost:{yourport}/js/another.js
, etc.如果您以这种方式进行设置,则可以
as long as you have a file called
index.js
in the js folder.