Tôi cũng phải đối mặt với vấn đề tương tự trong express 3.Xvà node 0.6.16. Các giải pháp đưa ra ở trên sẽ không hoạt động cho phiên bản mới nhất express 3.x. Họ loại bỏ app.registerphương thức và thêm app.enginephương thức. Nếu bạn đã thử giải pháp trên, bạn có thể gặp phải lỗi sau.
node.js:201
throw e; // process.nextTick error, or 'error' event on first tick
^
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)
Để thoát khỏi thông báo lỗi. Thêm dòng sau vàoapp.configure function
app.engine('html', require('ejs').renderFile);
Lưu ý: bạn phải cài đặt ejscông cụ mẫu
npm install -g ejs
Thí dụ:
app.configure(function(){
.....
// disable layout
app.set("view options", {layout: false});
app.engine('html', require('ejs').renderFile);
....
app.get('/', function(req, res){
res.render("index.html");
});
Lưu ý: Giải pháp đơn giản nhất là sử dụng mẫu ejs làm công cụ xem. Ở đó bạn có thể viết HTML thô trong * .ejs xem tệp.