简体   繁体   English

为什么在公用文件夹中找不到我的JavaScript文件?

[英]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.

相关问题 任何使用JavaScript将文件上传到我的Google Drive公用文件夹的人 - Anyone to upload a file to my Google Drive public folder using JavaScript 公用文件夹文件中的“找不到类&#39;Auth&#39;” - “Class 'Auth' not found” in file of public folder 为什么我的公用文件夹中的文件得到404 - Why my files in public folder gets 404 我可以使用jasminerice和rails 3.2.8测试我的公用文件夹中的javascript文件吗? - Can I test a javascript file inside my public folder with jasminerice and rails 3.2.8? 如何从我的laravel控制器获取价值并将其设置在公用文件夹的javascript文件中? - How to get value from my laravel controller and set it in the javascript file in public folder? Laravel 5.6在公用文件夹“找不到页面”中添加JavaScript库 - Laravel 5.6 adding javascript library in public folder “Page not found” 为什么我的 Node.js 网站在公用文件夹中找不到页面? - Why is my Node.js website not finding pages in public folder? 如何将javascript文件从公用文件夹导入SRC文件夹并使用该文件的功能 - How to import javascript file from public folder to SRC folder and use the function of that file 为什么我的 javascript 代码没有显示“未找到”? - Why my javascript code doesnt show "Not Found"? 为什么我的 javascript 函数没有被它嵌入的页面找到? - Why is my javascript function not found by the page it is embedded in?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM