简体   繁体   English

express.static() 不从不是“/”的路由器路径中的公共文件夹中提供文件

[英]express.static() not serving files from public folders in router paths that are not "/"

Express.static config: Express.static 配置:

app.use(express.static(__dirname + "/public"));

File Structure:文件结构:

--public
  --assets
  --js
    --[js scripts]
  --stylesheets
    --[css files]

Routes:路线:

const shopRoutes = require('./routes/shopRoutes')
app.use('/', shopRoutes)

const itemApiRoutes = require('./routes/itemApiRoutes')
app.use('/api/shopitems', itemApiRoutes)

const logSignRoutes = require('./routes/logSignRoutes')
app.use('/account', logSignRoutes)

The issue is that all ejs files in the root path at "localhost:3000/" (shopRoutes) calls for the following css file like so and works perfectly fine:问题是“localhost:3000/”(shopRoutes)根路径中的所有 ejs 文件都像这样调用以下 css 文件并且工作得很好:

<link rel="stylesheet" type="text/css" href="stylesheets/header.css">

Using Express Router, the ejs file at the path "localhost:3000/account/login" calls for the same css file with the exact same syntax but gets the error:使用 Express Router,路径 "localhost:3000/account/login" 上的 ejs 文件使用完全相同的语法调用相同的 css 文件,但得到错误:

Cannot GET /account/login/stylesheets/header.css/

Am I not understanding how express.static serves static files or am I doing something incorrectly?我是不是不明白 express.static 如何提供静态文件,或者我做错了什么?

If you specify a relative URL such as:如果您指定一个相对 URL,例如:

href="stylesheets/header.css" 

then the browser adds the path of the web page you're in to that and requests that combined path from your server.然后浏览器将您所在网页的路径添加到该路径中,并从您的服务器请求该组合路径。 Unless your web page is at the top level of your web site and thus has no path, it will not work properly.除非您的网页位于网站的顶层并且因此没有路径,否则它将无法正常工作。 I want to emphasize, this is the browser doing this, not Express.我想强调的是,这是浏览器在做这件事,而不是 Express。 So, if you're in a web page with this URL:因此,如果您在具有此 URL 的网页中:

http://localhost:3000/account/login

And, the browser sees:而且,浏览器会看到:

<link rel="stylesheet" type="text/css" href="stylesheets/header.css">

It will end up combine the path of the web page它最终会结合网页的路径

/account/login

with the relative URL you in <link> tag and as you've found out, it will request:使用<link>标记中的相对 URL,正如您发现的那样,它将请求:

/account/login/stylesheets/header.css

When, express.static() sees that URL, that will not match anything in yourexpress.static()看到该 URL 时,它将与您的

__dirname + "/public"

directory hierarchy so it will not be found.目录层次结构,所以它不会被找到。


Instead, you want to specify a leading slash:相反,您要指定一个前导斜杠:

<link rel="stylesheet" type="text/css" href="/stylesheets/header.css">

That tells the browser not add any path to the URL and it will send a request to your server for:这告诉浏览器不要向 URL 添加任何路径,它会向您的服务器发送请求:

/stylesheets/header.css

When express.static() gets that request, it combines that withexpress.static()收到该请求时,它会将其与

__dirname + "/public"

and will end up looking for a file并且最终会寻找一个文件

__dirname + "/public" + "/stylesheets/header.css"` 

which will be found in your public directory hierarchy and will work.这将在您的公共目录层次结构中找到并且将起作用。

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

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