TypeError:尝试在Express JS中使用Mustache时this.engine不是函数 [英] TypeError: this.engine is not a function when trying to use Moustache in Express JS

查看:96
本文介绍了TypeError:尝试在Express JS中使用Mustache时this.engine不是函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

作为我在NodeJS上尝试的第一件事,我正在构建一个简单的应用程序,该应用程序显示一个HTML页面,该页面告诉访问者其IP地址.

As the first thing I ever try on NodeJS, I'm building a simple app that displays a HTML page which tells visitors their IP address.

这是它的样子

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>

这是每次发送请求时控制台中显示的内容:

And here's what I get in the console every time I send a request:

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. 怎么了?

I already setup frontPage.html inside views/ and I already installed Moustache from NPM (npm install mu2 --save). What's wrong with it?

推荐答案

我最终绕过Express的模板系统,并使用Mustache自己的compileAndRender().像这样:

I ended up bypassing Express' template system and use Mustache's own compileAndRender(). Like this:

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");
});

现在可以使用.

这篇关于TypeError:尝试在Express JS中使用Mustache时this.engine不是函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆