当url中有参数时,表达样式表404错误

Ben*_*edt 0 css parameters node.js express pug

在localhost上使用express 3.5.1我在app.js文件中

app.use(express.static(path.join(__dirname, 'public')));
Run Code Online (Sandbox Code Playgroud)

我的style.css文件在public/stylesheets中

在我的page.jade文件中

doctype html
html
    head
        link(rel = "stylesheet" href = "stylesheets/style.css")
    body
        // more stuff here
Run Code Online (Sandbox Code Playgroud)

最后,在我的路线中,我有

app.get('/path', dostuff);
app.get('/path/:param', dostuff);

function dostuff(req,res) {
    res.render('page');
}
Run Code Online (Sandbox Code Playgroud)

现在,当我去localhost:3000/path时,一切都很好,包含了style.css.但是我转到localhost:3000/path/anything,页面仍然呈现,但是我碰巧包含的任何样式表都会出现404错误,即使函数完全相同.为什么会这样?对于这个具体案例,有些人似乎无法找到答案.

Ant*_*Chu 5

因为http://localhost:3000/path/foo,浏览器使用http://localhost:3000/path基本URL,所以它试图请求,http://localhost:3000/path/stylesheets/style.css因为它stylesheets/style.css是一个相对链接.用a前缀/将其转换为绝对链接而不是......

link(rel = "stylesheet" href = "/stylesheets/style.css")
Run Code Online (Sandbox Code Playgroud)