简体   繁体   English

在expressjs中使用响应对象提供HTML文件时,如何包含外部JavaScript文件?

[英]How do I include an external JavaScript file when serving an HTML file with a response object in expressjs?

My express app serves an HTML page from my disk upon the initial GET (ie, if I hit " http://localhost:3000/ " in the browser). 我的快递应用程序在初始GET时从我的磁盘提供HTML页面(即,如果我在浏览器中点击“ http:// localhost:3000 / ”)。 Now I would like to access a JavaScript file which is in the same location in the disk as the HTML file. 现在我想访问一个与HTML文件位于磁盘相同位置的JavaScript文件。 When I try to include it in 'index.html' by using 当我尝试使用时将其包含在'index.html'中

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

or 要么

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

or 要么

 <script src="~/MyAbsolutePath/myJavaScriptFile.js" type="text/javascript"</script>

it doesn't work. 它不起作用。 The myJavaScriptFile.js file is never reached. 永远不会到达myJavaScriptFile.js文件。

My express app looks like this: 我的快递应用看起来像这样:

 var express = require('express')
 var testMethod = require('./test')
 var app = express()
 app.use(bodyParser.urlencoded({ extended:false }));

 var server = app.listen(3000, function () {

 var host = server.address().address
 var port = server.address().port

 console.log('Example app listening at http://%s:%s', host, port)

 })

 app.get('/', function (req, res) {
 console.log('In /');
 res.sendFile(__dirname + '/index.html');
 })

Express app is serving 'index.html' using the reference path '__dirname' + '/index.html' using res.sendFile function. Express应用程序使用res.sendFile函数使用引用路径'__dirname'+'/ index.html'为'index.html'提供服务。 (I am beginning to feel that this is a bad way of doing it. Please let me know if you think so too). (我开始觉得这是一种糟糕的做法。如果你这么想,请告诉我)。

Also as we can see in the express app, an external JavaScript file called 'test' which is in the same location as 'index.html' and 'express.js' is being included without any issues. 另外,正如我们在快递应用程序中看到的那样,包含“test”的外部JavaScript文件与“index.html”和“express.js”位于同一位置,没有任何问题。 Could anyone please shed light on what's actually happening in the background? 有谁能请说明背景中实际发生的事情? What exactly would be reference path for the JavaScript file that I can give in my 'index.html' if it is being served by express app? 如果由快递应用程序提供,我可以在我的'index.html'中提供的JavaScript文件的参考路径究竟是什么? Thank you. 谢谢。

Serving files, such as images, CSS, JavaScript and other static files is accomplished with the help of a built-in middleware in Express - express.static. 借助Express - express.static中的内置中间件,可以完成服务文件,如图像,CSS,JavaScript和其他静态文件。

Pass the name of the directory, which is to be marked as the location of static assets, to the express.static middleware to start serving the files directly. 将目录的名称(将标记为静态资产的位置)传递给express.static中间件,以便直接开始提供文件。 For example, if you keep your images, CSS, and JavaScript files in a directory named public, you can do this: 例如,如果将图像,CSS和JavaScript文件保存在名为public的目录中,则可以执行以下操作:

app.use(express.static('public'));

Now, you will be able to load the files under the public directory: 现在,您将能够加载公共目录下的文件:

http://localhost:3000/images/kitten.jpg
http://localhost:3000/css/style.css
http://localhost:3000/js/app.js
http://localhost:3000/images/bg.png
http://localhost:3000/hello.html

More Detail Here 更多细节在这里

Happy Helping! 快乐帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM