res.sendFile(path.join(__ dirname,'../public','mapv02.html')); [英] res.sendFile(path.join(__dirname, '../public', 'mapv02.html'));

查看:75
本文介绍了res.sendFile(path.join(__ dirname,'../public','mapv02.html'));的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

mapv02.html严格发送,但找不到mapv01.html中的".js"!

mapv02.html is sending rigthly,but the '.js' in mapv01.html can not be found!

//routes/restapi.js

//routes/restapi.js

res.sendFile(path.join(__dirname, '../public', 'mapv02.html'));

//public/mapv02.html

//public/mapv02.html

<script type="text/javascript" src="js/jquery-2.1.4.js"></script>

推荐答案

要提供静态文件(例如图像,CSS文件和JavaScript文件),请使用Express中内置的express.static内置中间件功能.

To serve static files such as images, CSS files, and JavaScript files, use the express.static built-in middleware function in Express.

将包含静态资产的目录的名称传递给express.static中间件函数,以开始直接提供文件.

Pass the name of the directory that contains the static assets to the express.static middleware function to start serving the files directly.

示例:-使用以下代码在名为public的目录中提供图像,CSS文件和JavaScript文件:

Example:- Use the following code to serve images, CSS files, and JavaScript files in a directory named public:

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

现在,您可以加载公共目录中的文件:

Now, you can load the files that are in 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

此后,脚本将成功加载.

After this your script will be loaded successfully.

<script type="text/javascript" src="js/jquery-2.1.4.js"></script>

有关更多参考,请参见此链接- http://expressjs.com/en/starter/static-files.html

For more reference see this link - http://expressjs.com/en/starter/static-files.html

希望这将有助于解决您的查询!

这篇关于res.sendFile(path.join(__ dirname,'../public','mapv02.html'));的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆