【发布时间】:2016-12-03 16:48:33
【问题描述】:
作为我在 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
我已经在views/ 中设置了frontPage.html,并且我已经从 NPM (npm install mu2 --save) 安装了 Mustache。
它有什么问题?
【问题讨论】:
-
经过一番研究,我发现 Express 与 Mustache 的兼容性存在一些不一致的地方。即使网站上的指南说它兼容,Mustache 也不在此列表中:github.com/expressjs/express/…
标签: node.js express template-engine mustache