css - 在 express 中获取 css 时出错

标签 css express pug

当我尝试访问 http://localhost:3000/contato/1 时,我一直收到以下错误消息

GET http://localhost:3000/contato/static/bootstrap/dist/css/bootstrap.min.css 404 (Not Found)

这是由这条路线定义的:

app.route('/contato/:id')
    .get(autenticar, contatos.show);

在其他没有 :id 参数的路由中,CSS 渲染良好。

我正在使用 Pug 开发页面,并通过公共(public)文件夹提供静态文件,使用:

app.use('/static', express.static(__dirname + '/public'));

我在每个页面中扩展的 header.pug。

doctype html
html
head
    link(href='static/bootstrap/dist/css/bootstrap.min.css' rel='stylesheet')
    meta(charset='utf-8')
    meta(name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no")
    title Ntalk - Agenda de Contatos
body
    div(class='container')
        block content

出错的路线:

显示哈巴狗

include ../header
  block content
    div(class='row')
      div(class='col-md-6 offset-md-3')
        section(class='container')
          header
            h2 Ntalk - Dados do contato

最佳答案

您应该在 header.pug

中更改指向 css 的链接(通过在开头添加/)
link(href='/static/bootstrap/dist/css/bootstrap.min.css' rel='stylesheet')

否则与当前页面相关

关于css - 在 express 中获取 css 时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41653570/

相关文章:

html - 可拉伸(stretch)至全尺寸的响应图像

node.js - ExpressJS mysql 更新错误

javascript - 引用的对象和虚拟 getter 在 pug 布局文件中返回未定义,但从数据库查询成功

javascript - 如何在 Node.js 的全局数组中保存 mysql 查询

node.js - Jade - 意外的标识符

javascript - 部署在谷歌云平台与本地服务器上时,站点范围的字体不同

javascript - 打印特定 div 时出现问题

javascript - https 上的 js、css 和图像未加载

css - Less - 动态样式表语言?

html - 背景图像的占位符图像?