所以我在快递项目中加载样式表时遇到问题。当我使用express 2.5.8时,我的样式被正确加载,但是当我更新到3.x时,样式开始无法加载。视图得到渲染,但没有任何样式。
我正在使用节点,表达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
答案 0 :(得分:3)
我相信Express 3中默认情况下布局渲染设置为false
,因此请尝试在配置中添加以下内容:
app.set('view options', { layout: true });
更新: The migration guide说明布局和部分的概念已被完全删除。相反,请使用Jade's template inheritance。
<强> /视图/布局强>
!!! 5
head
// all your header elements
body
block content
<强> /视图/家强>
在顶部添加以下内容:
extends layout
block content
// the stuff you want to have in your content block
有关详细信息,请查看this guide。
答案 1 :(得分:2)
app.use(express.static(__dirname + '/public'));
必须在
之前app.use(app.router);