作为我在 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
inside views/
我已经从 NPM 安装了 Mustache (npm install mu2 --save
)。
它出什么问题了?