Как первое, что я когда-либо пытался использовать NodeJS, я создаю простое приложение, которое отображает страницу HTML, которая сообщает посетителям об их IP-адресе.TypeError: this.engine не является функцией при попытке использования Mustache в Express JS
Вот как это выглядит
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/
и я a уже установлен Усы из НПМ (npm install mu2 --save
). Что в этом плохого?
После некоторых исследований я обнаружил некоторые несоответствия в отношении совместимости Express с Усы. Даже когда руководство на веб-сайте говорит, что оно совместимо, Mustache отсутствует в этом списке: https://github.com/expressjs/express/wiki?_ga=1.74621138.1527575629.1480681917#template-engines – starleaf1