[英]Why my javascript file is not found in the public folder?
I was trying to make my first web and I had the following route in my node.js code: 我试图制作我的第一个网站,并且在我的node.js代码中遵循以下路线:
app.get("/contact_form", function(req, res){
res.render("new_contact");
});
new_contact was including correctly my javascript file and I was doing that with: new_contact正确包含了我的JavaScript文件,而我使用以下方法进行了操作:
<script type="text/javascript" src="scripts/jquery.min.js"></script>
Now I have changed my route to: 现在,我将路线更改为:
app.get("/contact_form/new", function(req, res){
res.render("new_contact");
});
And it stopped working; 它停止工作了; I had to change the including with a / before scripts to make it work again: 我必须用/更改脚本之前的包含,以使其再次起作用:
<script type="text/javascript" src="/scripts/jquery.min.js"></script>
Please, could someone explain me thoroughly what it is going on? 拜托,有人能彻底解释一下发生了什么吗? I have taken a look to several answers and websites but all explanations are very vague kind of: 1. The slash means the root. 我看了几个答案和网站,但是所有的解释都是非常模糊的:1.斜线表示词根。 2. Without the slash is a relative path 2.没有斜线是相对路径
And so son. 儿子
Thank you in advance. 先感谢您。
If you have the url: https://localhost/contact_form
, then a relative path ( src="scripts/jquery.min.js"
) will look for the jquery using: 如果您具有url: https://localhost/contact_form
,则相对路径( src="scripts/jquery.min.js"
)将使用以下命令查找jquery:
https://localhost/scripts/jquery.min.js https://localhost/scripts/jquery.min.js
Once you made the change, you had this url: https://localhost/contact_form/new
. 进行更改后,您将拥有以下URL: https://localhost/contact_form/new
。 If you continue to use the relative path, the browser will look for jquery using: 如果继续使用相对路径,浏览器将使用以下命令查找jquery:
https://localhost/contact_form/scripts/jquery.min.js https://localhost/contact_form/scripts/jquery.min.js
Which wouldn't work, because it isn't there. 这不起作用,因为它不存在。 It is at the root . 它是根源 。
So changing to the root path ( src="/scripts/jquery.min.js"
) will always start from the root path (just after the domain): 因此,更改为根路径( src="/scripts/jquery.min.js"
)将始终从根路径开始(仅在域之后):
https://localhost/scripts/jquery.min.js https://localhost/scripts/jquery.min.js
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.