【问题标题】:Error Loading Stylesheets in Express 3.x在 Express 3.x 中加载样式表时出错
【发布时间】:2013-01-02 09:28:35
【问题描述】:

所以我在我的 express 项目中加载样式表时遇到问题。当我使用 express 2.5.8 时,我的样式被正确加载,但是当我更新到 3.x 时,样式开始无法加载。视图被渲染但没有任何样式。

我正在使用 node、express 3.x、jade 和 bootstrap。我的样式在public/stylesheets/*

更新:由于某种原因,layout.jade 似乎没有被渲染。

这是我的server.js

require('coffee-script');
/**
 * Module dependencies.
 */

var express = require('express'),
            flash = require('connect-flash');

var app = module.exports = express.createServer();

// Configuration

app.configure(function(){
  app.set('views', __dirname + '/views');
  app.set('view engine', 'jade');
  app.set('port', 3000);
  app.use(express.bodyParser());
  app.use(express.methodOverride());
  app.use(flash());
  app.use(require('connect-assets')());
  app.use(app.router);
  app.use(express.static(__dirname + '/public'));
});

app.configure('development', function(){
  app.use(express.errorHandler({ 
    dumpExceptions: true, 
    showStack: true 
  }));
});

app.configure('test', function () {
  app.set('port', 3001);
});

app.configure('production', function(){
  app.use(express.errorHandler());
});

// Routes

require('./apps/landing-page/routes')(app);
require('./apps/authentication/routes')(app);
require('./apps/home/routes')(app);
require('./apps/register/routes')(app);


app.listen(3000);
console.log("Express server listening on port %d in %s mode", app.settings.port, app.settings.env);

这是我的主要layout.jade 视图:

!!!
html
  head
    title= title
    script(src='http://code.jquery.com/jquery-latest.js')
    link(rel='stylesheet', href='/stylesheets/bootstrap.css')
    link(rel='stylesheet', media='screen', href='/stylesheets/bootstrap-responsive.css')
    link(rel='stylesheet', href='/stylesheets/app.css')

    script(src='javascripts/bootstrap.js')

    |   <!--[if lt IE 9]>
    |   <link rel="stylesheet" href="stylesheets/ie.css">
    |   <![endif]-->

    |   <!-- IE Fix for HTML5 Tags -->
    |   <!--[if lt IE 9]>
    |     <script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script>
    |   <![endif]-->

  body 
    !=body


script(src='javascripts/app.js')

这是我的一条路线:

routes = (app) ->
    app.get '/home', (req, res) ->
        res.render "#{__dirname}/views/home",
        title: 'Home | SiteName'
        stylesheet: 'home'


module.exports = routes

【问题讨论】:

    标签: node.js express pug


    【解决方案1】:

    我相信 Express 3 中的布局渲染默认设置为 false,因此请尝试将以下内容添加到您的配置中:

    app.set('view options', { layout: true });
    

    更新:The migration guide 声明布局和局部的概念已被完全删除。请改用Jade's template inheritance

    /views/layout

    !!! 5
    head
        // all your header elements
    body
        block content
    

    /views/home

    在顶部添加以下内容:

    extends layout
    
    block content
        // the stuff you want to have in your content block
    

    欲了解更多信息,请查看this guide

    【讨论】:

    • 我可能只是将 css/js 声明放在每个单独视图的头部,但我觉得这有点 hacky。我的布局应该首先呈现...
    • @ConnorBlack 更新了有关如何用 Jade 块替换布局的信息
    • @ConnorBlack 不客气!另外,您可能想看看我的项目express-assets。检查示例以了解它是如何工作的:)
    【解决方案2】:
    app.use(express.static(__dirname + '/public')); 
    

    必须在

    之前
    app.use(app.router);
    

    【讨论】:

      猜你喜欢
      • 2013-02-25
      • 2017-09-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-01-28
      • 1970-01-01
      • 2013-01-21
      • 2017-12-09
      相关资源
      最近更新 更多