简体   繁体   English

app.use(express.static("public")) 如何处理嵌套的 URL?

[英]How does app.use(express.static("public")) work with nested URL's?

I am quite new to node and I am trying to wrap my head around how the express.static middleware is working.我对 node 很陌生,我正试图围绕 express.static 中间件的工作方式进行思考。 In my views folder I have some href's like this:在我的视图文件夹中,我有一些像这样的 href:

<link href="vendor/bootstrap/css/bootstrap.min.css" rel="stylesheet" />

My application is able to grab these files form URLS's such as localhost/about, localhost/contact etc. However it will not grab files if the end point is something like localhost/form/new.我的应用程序能够从 URLS 获取这些文件,例如 localhost/about、localhost/contact 等。但是,如果端点类似于 localhost/form/new,它不会获取文件。 Is express.static getting these static assets from localhost/somefile and when there is a nested URL it defaults to /form/somefile (which won't work)? express.static 是否从 localhost/somefile 获取这些静态资产,并且当存在嵌套 URL 时,它默认为 /form/somefile (这不起作用)? I am aware that if you put a '/' before vendor it will work from any level, why is this?我知道如果你在 vendor 前加上一个“/”,它可以在任何级别工作,这是为什么? Thank you.谢谢你。

app.use(express.static(“public”));

That is a way of saying "hey express, look if any incoming requests (like GET /bundle.css matches a file on that directory, if so, send it!".这是一种说“嘿,表达,看看是否有任何传入请求(如 GET /bundle.css 与该目录中的文件匹配,如果是,发送它!”)的方式。

Any file on that directory, you should be able to access under /.该目录上的任何文件,您都应该可以在 / 下访问。 If you're serving from your localhost and have bundle.css on public folder, you can visit http://localhost/bundle.css .如果您从本地主机提供服务并且在公共文件夹中有 bundle.css,您可以访问http://localhost/bundle.css

Any asset you're trying to get from public folder, should start with / meaning look for an absolute path (in this case, the path public folder is serving which is /).您尝试从公用文件夹获取的任何资产都应以 / 开头,表示查找绝对路径(在这种情况下,公用文件夹所服务的路径为 /)。

Update更新

There is another way of doing that using relative path which is not recommendable.还有另一种使用相对路径的方法,这是不推荐的。

If you're in /about/index.html and you're trying to get /css/bundle.css (under public's folder)如果您在/about/index.html 中并且您正在尝试获取/css/bundle.css (在 public 文件夹下)

Absolute Path:绝对路径:

<link rel="stylesheet" href="/css/bundle.css">

Relative Path: (not recommendable)相对路径:(不推荐)

<link rel="stylesheet" href="../css/bundle.css">

The only thing to keep in mind is to make a slash mark before the static file address.唯一要记住的是在静态文件地址之前做一个斜线标记。


 <link rel="shortcut icon" href="/img/favicon.ico" type="image/x-icon"> <link rel="stylesheet" href="/css/style.css" type="text/css">


If you do not put this slash before the address, you will have a problem loading styles, images, etc. in the nested addresses.如果地址前不加这个斜杠,嵌套地址中加载样式、图片等会出现问题。

For example, if you use the following addressing instead of the above operation, you will definitely have problems in the nested URL.例如,如果你使用下面的寻址而不是上面的操作,那么嵌套的URL肯定会出现问题。

 <link rel="shortcut icon" href="img/favicon.ico" type="image/x-icon"> <link rel="stylesheet" href="css/style.css" type="text/css">


This is especially useful on page 404.这在第 404 页特别有用。

I hope it was useful我希望它有用

暂无
暂无

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

相关问题 使用 app.use(express.static(&#39;public&#39;)) 时如何测试 Express 应用程序; - How to test an Express application when using app.use(express.static('public')); 如何关闭app.use(express.static(&#39;/ public&#39;)); 快递js - How to turn off app.use(express.static('/public')); Express js app.use(express.static("public")) 是否为每个请求调用中间件? - Does app.use(express.static("public")) call the middleware for every request? 如何配置express.js的app.use(express.static(…)`? - How to configure express.js's `app.use(express.static(…)`? app.use(“ /”,express.static)和app.use(express.static)之间有区别吗? - Is there a difference between app.use(“/”, express.static) and app.use(express.static)? 为什么省略行app.use(express.static(__ dirname,&#39;public&#39;))会阻止我的html页面加载css文件? - Why does omitting the line app.use(express.static(__dirname, 'public')) stop my html pages from loading css files? app.use(express.static(path.join(__dirname, 'public'))); ReferenceError:未定义路径是什么问题? - app.use(express.static(path.join(__dirname, 'public'))); ReferenceError: path is not defined what is issue? 使用 app.use(express.static(__dirname, 'public')); 上传后我无法访问图像 - I can't access image after upload with used app.use(express.static(__dirname, 'public')); 为什么在使用app.use(express.static(path.join(...)))之前无法启动Express Middleware? - Why does Express Middleware not fire when preceded by app.use(express.static(path.join(…))); 如果app-是子应用程序,则`app.use(express.static`似乎不起作用 - `app.use(express.static` seems to not working if app - is subapplication
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM