TypeError:尝试在Express JS中使用Mustache时,this.engine不是函数

作为我在NodeJS上尝试的第一件事,我构build了一个简单的应用程序,显示一个HTML页面,告诉访问者他们的IP地址。

这是它的样子

var express = require('express'); var app = express(); app.set('view engine', 'mu2'); app.get('/', function (req, res) { res.setHeader('Content-Type', 'text/html'); // Do I have to do this? I'm not sure. res.render('frontPage.html', { ip: req.ip }); res.send(); }); app.listen(8080, function() { console.log("Listening on port 8080"); }); 

以下是/views/frontPage.html样子:

 <!DOCTYPE html> <html> <head> <title>Hello, World!</title> </head> <body> <h1>Hello, World!</h1> <hr> <p>If you're reading this, the NodeJS setup is working. Check your developer console. See if there's any HTTP error in there.</p> <p>Anyway, your IP address is {{ip}}</p> </body> </html> 

每当我发送一个请求时,我在控制台中得到的是:

 TypeError: this.engine is not a function at View.render (/Users/macuser/NodeJS/hello/node_modules/express/lib/view.js:126:8) at tryRender (/Users/macuser/NodeJS/hello/node_modules/express/lib/application.js:639:10) at EventEmitter.render (/Users/macuser/NodeJS/hello/node_modules/express/lib/application.js:591:3) at ServerResponse.render (/Users/macuser/NodeJS/hello/node_modules/express/lib/response.js:960:7) at /Users/macuser/NodeJS/hello/index.js:8:9 at Layer.handle [as handle_request] (/Users/macuser/NodeJS/hello/node_modules/express/lib/router/layer.js:95:5) at next (/Users/macuser/NodeJS/hello/node_modules/express/lib/router/route.js:131:13) at Route.dispatch (/Users/macuser/NodeJS/hello/node_modules/express/lib/router/route.js:112:3) at Layer.handle [as handle_request] (/Users/macuser/NodeJS/hello/node_modules/express/lib/router/layer.js:95:5) at /Users/macuser/NodeJS/hello/node_modules/express/lib/router/index.js:277:22 

我已经在views/里面设置了frontPage.html而且我已经从NPM安装了Mustache( npm install mu2 --save )。 它出什么问题了?

我最终绕过Express模板系统,并使用Moustache自己的compileAndRender() 。 喜欢这个:

 var express = require('express'); var app = express(); var mu2 = require('mu2'); mu2.root = __dirname + '/views'; app.get('/', function (req, res) { var htmlStream = mu2.compileAndRender('frontPage.html', {ip: req.ip}); htmlStream.pipe(res); }); app.listen(8080, function () { console.log("Listening on port 8080"); }); 

它现在有效。

您需要将您的文件扩展名从.html更改为.mu2。 res.render('frontPage.mu2', { ip: req.ip}); 因为它是一个胡须文件而不是HTML文件。 您也可以将文件扩展名closures,因为您将默认视图渲染器设置为mu2,如果没有提供文件扩展名,则express将使用该扩展名作为渲染引擎。 像这样… res.render('frontPage', {ip: req.ip}); 。 请注意,第一部分是文件path“/ frontPage”,第二部分是您传递给模板的本地对象。 您可以在.mu2文件中访问该对象的ip属性,如{{ip}} 。 胡须返回呈现的HTML来表示,res.render将其发送到客户端。

另外,你不需要res.send(),因为res.render()呈现一个视图,并把呈现的HTMLstring发送给客户端,并且由于text / html也是响应或者inputString,所以你不需要需要res.setHeader('Content-Type','text-html'); 但是。 ;)

从expressjs.com

res.render(view [,locals] [,callback])呈现一个视图并将呈现的HTMLstring发送到客户端。 可选参数:

当地人 ,一个对象的属性定义视图的局部variables。

callback ,一个callback函数。 如果提供,该方法返回可能的错误和呈现的string,但不执行自动响应。 发生错误时,该方法会在内部调用next(err)。

view参数是一个string,它是要呈现的视图文件的文件path。 这可以是绝对path,也可以是相对于视图设置的path。 如果path不包含文件扩展名,则视图引擎设置将确定文件扩展名。 如果path包含文件扩展名,那么Express将加载指定模板引擎的模块(通过require()),并使用加载的模块的__express函数进行渲染。