2013-08-27 88 views
0

我想使用其默認實現運行Express 3.3.x。 Express使用它的路由模塊,所以我必須做什麼,如果JS和CSS可以被任何路由中的任何視圖訪問?與express 3和nodejs一起使用piler

/** 
* Module dependencies. 
*/ 
var express = require('express'); 
var http = require('http'); 
var path = require('path'); 

var piler = require('piler'); 

var mongoose = require('mongoose'); 

var config = require('./config'); 

var app = exports.app = express(); 
var js = piler.createJSManager(); 
var css = piler.createCSSManager(); 
var srv = require('http').createServer(app); 

// all environments 
js.bind(app,srv); 
css.bind(app,srv); 

app.set('port', process.env.PORT || 3000); 
app.set('views', __dirname + '/views'); 
app.set('view engine', 'jade'); 
app.use(express.favicon()); 
app.use(express.logger('dev')); 
app.use(express.bodyParser()); 
app.use(express.methodOverride()); 
app.use(express.cookieParser('your secret here')); 
app.use(express.session()); 
app.use(app.router); 
app.use(require('less-middleware')({ src: __dirname + '/public' })); 
app.use(express.static(path.join(__dirname, 'public'))); 

js.addUrl("http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.js"); 

// development only 
if ('development' == app.get('env')) { 
    app.use(express.errorHandler()); 
} 

/** 
* Routes 
*/ 
var routes = require('./routes'); 
app.get('/', routes.index); 

srv.listen(app.get('port'), function(){ 
    console.log('Express server listening on port ' + app.get('port')); 
}); 

在堆垛機的例子:

app.get("/", function(req, res){ 
    res.render("index.jade", { 
     layout: false, 
     js: js.renderTags(), 
     css: css.renderTags() 
    }); 
}); 

這工作。但我有

app.get('/', routes.index); 

所以我必須做的,那js.renderTags()適用於所有視圖?

+1

我沒有問題,這裏的玉器模板有什麼問題,不允許這樣做? – Kosmetika

回答

2

如果你想傳遞變量來呈現,您可以使用res.locals

app.use(function(req,res,next){ 
    res.locals.layout= false; 
    res.locals.js= js.renderTags(); 
    res.locals.css= css.renderTags(); 
    next(); 
}); 

使用此路由器之前,但不覆蓋你的當地人(res.locals={...}