css - url中有参数时express stylesheet 404错误

标签 css node.js express parameters pug

在本地主机上使用 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/

相关文章:

php - 每天存储数百万条记录并可出于统计目的进行分组的数据的最佳方法是什么?

javascript - NodeJs 在执行测试用例时用 Jest 错误表达 MongoDB

node.js - 如何将 Google oAuth 与 Node.js 后端和 Angular 前端结合使用?

javascript - Dhtmlx Scheduler 时间线为周末上色

javascript - 将表格标题列 th 分成两个 th

html - 2 个背景,1 个在顶部,1 个在屏幕或页脚

node.js - 从 NodeJs 中的一组 url 图片创建动画 Gif

javascript - AWS SDK 文件通过 Node/Express 使用流 PassThrough 上传到 S3 - 文件总是损坏

javascript - 在 Firebase Functions HTTPS 请求中使用 Express 中间件

html - 调整浏览器大小时停止侧边栏向下移动