在本地主机上使用 express 3.5.1 我的 app.js 文件中有
app.use(express.static(path.join(__dirname, 'public')));
我的 style.css 文件在 public/stylesheets 中
在我的 page.jade 文件中有
doctype html
html
head
link(rel = "stylesheet" href = "stylesheets/style.css")
body
// more stuff here
最后,在我的 route 我有
app.get('/path', dostuff);
app.get('/path/:param', dostuff);
function dostuff(req,res) {
res.render('page');
}
现在,当我转到 localhost:3000/path 时,一切正常,并且包含 style.css。但是我转到 localhost:3000/path/anything,页面仍然呈现,但是我碰巧包含的任何样式表都出现 404 错误,即使功能完全相同。为什么会这样?是否有一些四处寻找似乎无法找到这个特定案例的答案。
最佳答案
对于 http://localhost:3000/path/foo
,浏览器使用 http://localhost:3000/path
作为基本 URL,所以它正在尝试请求 http://localhost:3000/path/stylesheets/style.css
因为 stylesheets/style.css
是一个相对链接。用 /
前缀将其变成绝对链接...
link(rel = "stylesheet" href = "/stylesheets/style.css")
关于css - url中有参数时express stylesheet 404错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24132472/