NodeJS + Express:为不同的URL提供静态文件
问题描述:
我正在使用node.js + express来提供静态文件(CSS + JS).此时,静态目录配置为
I'm using node.js+express for serving static files (CSS+JS). At this time static dir is configured as
app.use(express.static(__dirname + '/static'));
在主模板布局中,我将静态文件加载为
In main templeate layout.jade I load static files as
link(href='css/bootstrap.css', rel='stylesheet')
一切都适用于/hello,/write,/:user等页面.但是,当我获得/bob/505b6833d3835d3705000001/edit之类的页面时,找不到静态文件.Firebug显示Node会为静态生成相同的路径,但是样式不适用于该页面.为什么?预先感谢!
Everything works fine with pages like /hello, /write, /:user. But when I get pages like /bob/505b6833d3835d3705000001/edit, static files cannot be found. Firebug shows Node generates the same path for static, but styles are not applied for the page. Why? Thanks in advance!
答
您应该使用带有斜杠的链接.
You should be using link with pre-slash.
link(href='/css/bootstrap.css', rel='stylesheet')
这应该照顾它.