作为我尝试使用NodeJS的第一件事,我正在构建一个显示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
我已在frontPage.html
内设置了views/
,我已经从NPM(npm install mu2 --save
)安装了Mustache。
怎么了?
答案 0 :(得分:1)
我最终绕过Express'模板系统并使用Mustache自己的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");
});
现在有效。
答案 1 :(得分:0)
您需要将文件扩展名从.html更改为.mu2。 res.render('frontPage.mu2', { ip: req.ip});
因为它是一个Mustache文件而不是HTML文件。您也可以保留文件扩展名,因为您将默认视图渲染器设置为mu2,如果没有提供文件扩展名,express将使用它作为渲染引擎。
像这样...... res.render('frontPage', {ip: req.ip});
。请注意,第一部分是文件路径'/ frontPage',第二部分是您传递给模板的本地对象。您可以在.mu2文件中访问该对象的ip属性,如{{ip}}
。 Mustache返回呈现的HTML表示,res.render在客户端上发送它。
此外,您不需要res.send(),因为res.render()呈现视图,并将呈现的HTML字符串发送到客户端,并且因为text / html也在响应或类型String上被假定,不需要res.setHeader('Content-Type','text-html');但要么。 ;)
res.render(view [,locals] [,callback]) 呈现视图并将呈现的HTML字符串发送到客户端。可选参数:
locals ,一个对象,其属性定义视图的局部变量。
回调,一个回调函数。如果提供,则该方法返回可能的错误和呈现的字符串,但不执行自动响应。发生错误时,该方法在内部调用next(err)。
view参数是一个字符串,它是要呈现的视图文件的文件路径。这可以是绝对路径,也可以是相对于视图设置的路径。如果路径不包含文件扩展名,则视图引擎设置将确定文件扩展名。如果路径确实包含文件扩展名,那么Express将为指定的模板引擎加载模块(通过require())并使用加载的模块的__express函数渲染它。