我不想使用 Express 默认自带的 Jade 模板引擎。我尝试按照本指南进行操作,但失败了:
http://blog.luksidadi.com/expressjs-underscore-template/
有问题的错误是:
node.js:201
throw e; // process.nextTick error, or 'error' event on first tick
^
Error: callback function required
at Function.engine (/home/me/blog/node_modules/express/lib/application.js:173:38)
at Object.<anonymous> (/home/tk/blog/app.js:28:5)
at Module._compile (module.js:432:26)
at Object..js (module.js:450:10)
at Module.load (module.js:351:31)
at Function._load (module.js:310:12)
at Array.0 (module.js:470:10)
at EventEmitter._tickCallback (node.js:192:40)
当我尝试使用以下命令启动服务器时,我得到了这个:
node app.js
如何解决这个问题?
应用程序.js:
/**
* Module dependencies.
*/
var express = require('express')
, routes = require('./routes')
, user = require('./routes/user')
, http = require('http')
, path = require('path');
var app = express();
app.configure(function(){
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(app.router);
app.use(express.static(path.join(__dirname, 'public')));
});
// Add these lines to register underscore template
var _ = require('underscore');
app.engine('.html', {
compile: function(str, options){
var compiled = require('underscore').template(str);
return function(locals) {
return compiled(locals);
};
}
});
app.configure('development', function(){
app.use(express.errorHandler());
});
app.get('/', routes.index);
app.get('/users', user.list);
http.createServer(app).listen(app.get('port'), function(){
console.log("Express server listening on port " + app.get('port'));
});
路线/index.js:
/*
* GET home page.
*/
exports.index = function(req, res){
res.render('index.html', { title: 'Express' });
};
布局.html:
< html >
< head >
< title ><%=title%>< /title >
< /head >
< body >
<%=body%>
< /body >
< /html >
索引.html:
Hello world