问题与明确的静态中间件和子目录
我有这个文件夹作为我的网站结构
-views
-指数
-post
-上市
-CSS
-js
-图片
-app.js
但是一个post链接可以是:
mywebsite.com/posts/a-link-example
所以,当我使用这样的快速静态中间件:
app.use(express.static(__dirname + '/public'));
它只能在没有“子目录”的页面中工作
example.com/home example.com/contact
但不是
example.com/posts/post-name
我当然可以使用:
app.use('/posts, express.static(__dirname + '/public'));
但有没有更好的方法来做到这一点?
不要使用相对path指向静态资源(如脚本和样式表),请使用绝对path:
<link rel="stylesheet" href="/css/style.css">
您可以更改以下静态资产链接:
<link rel="stylesheet" href="./css/style.css">
至:
<link rel="stylesheet" href="/css/style.css">