2

所以我在我的 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
4

2 回答 2

3

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

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

更新: 迁移指南声明布局和部分的概念已被完全删除。相反,使用Jade 的模板继承

/视图/布局

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

/视图/主页

在顶部添加以下内容:

extends layout

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

有关更多信息,请查看本指南

于 2013-01-02T01:17:30.513 回答
2
app.use(express.static(__dirname + '/public')); 

必须在之前

app.use(app.router);
于 2013-01-02T01:15:41.560 回答