在express 3.X
和node 0.6.16
中,我也遇到了同样的问题。上面给出的解决方法在最新版本的express 3.x
中不起作用。他们删除了app.register
方法并添加了app.engine
方法。如果您尝试上面的解决方法,则可能会遇到以下错误。
node.js:201
throw e;
^
TypeError: Object function app(req, res){ app.handle(req, res); } has no method 'register'
at Function.<anonymous> (/home/user1/ArunKumar/firstExpress/app.js:37:5)
at Function.configure (/home/user1/ArunKumar/firstExpress/node_modules/express/lib/application.js:399:61)
at Object.<anonymous> (/home/user1/ArunKumar/firstExpress/app.js:22:5)
at Module._compile (module.js:441:26)
at Object..js (module.js:459:10)
at Module.load (module.js:348:31)
at Function._load (module.js:308:12)
at Array.0 (module.js:479:10)
at EventEmitter._tickCallback (node.js:192:40)
为了消除这个错误消息,请在app.configure函数
中添加以下行
app.engine('html', require('ejs').renderFile);
注意:您需要安装ejs
模板引擎
npm install -g ejs
示例:
app.configure(function(){
.....
app.set("view options", {layout: false});
app.engine('html', require('ejs').renderFile);
....
app.get('/', function(req, res){
res.render("index.html");
});
注意: 最简单的解决方案是使用ejs模板作为视图引擎。在*.ejs视图文件中,您可以编写原始HTML。