level 1
应该可以:
这句进去:app.set('view engine', 'ejs');
是这个:
app.set = function(setting, val){
if (arguments.length === 1) {
// app.get(setting)
return this.settings[setting];
}
// set value
this.settings[setting] = val; // 所以应该和这个相关
// trigger matched settings
switch (setting) {
case 'etag':
debug('compile etag %s', val);
this.set('etag fn', compileETag(val));
break;
case 'query parser':
debug('compile query parser %s', val);
this.set('query parser fn', compileQueryParser(val));
break;
case 'trust proxy':
debug('compile trust proxy %s', val);
this.set('trust proxy fn', compileTrust(val));
break;
}
return this;
};
然后搜索找到这个:
app.render = function(name, options, fn){
var opts = {};
var cache = this.cache;
var engines = this.engines;
var view;
// support callback function as second arg
if ('function' == typeof options) {
fn = options, options = {};
}
// merge app.locals
mixin(opts, this.locals);
// merge options._locals
if (options._locals) mixin(opts, options._locals);
// merge options
mixin(opts, options);
// set .cache unless explicitly provided
opts.cache = null == opts.cache
? this.enabled('view cache')
: opts.cache;
// primed cache
if (opts.cache) view = cache[name];
// view
if (!view) {
view = new (this.get('view'))(name, {
defaultEngine: this.get('view engine'), // 这个就有意思了吧,吧它改成根据name的后缀名来判断用哪种不就可以了吗?对吧
root: this.get('views'),
engines: engines
});
if (!view.path) {
var err = new Error('Failed to lookup view "' + name + '" in views directory "' + view.root + '"');
err.view = view;
return fn(err);
}
// prime the cache
if (opts.cache) cache[name] = view;
}
// render
try {
view.render(opts, fn);
} catch (err) {
fn(err);
}
};
######
# 所以我们最后要做的只是 #
#######
1. 打开 node_modules/express/lib/application.js找到第500行:
把 this.get('view engine') 改成:
this.get(path.extname(name) + " engine") ? this.get(path.extname(name) + " engine") : this.get('view engine')
然后在上面加上
var path = require('path')
2. 在你的app里面加上
正确的
引擎支持
app.set('ejs engine', 'ejs');
app.set('jade engine', 'jade');
3. 在你res.render的第一个参数调用的时候记得加后缀。
比如:res.render('index.jade', { title:'主页'});
2014年12月04日 09点12分