【问题标题】:TypeError: this.engine is not a function when trying to use Moustache in Express JSTypeError: this.engine 在 Express JS 中尝试使用 Mustache 时不是函数
【发布时间】: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


【解决方案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");
});

现在可以了。

【讨论】:

    【解决方案2】:

    您需要将文件扩展名从 .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 也被假定为响应或字符串类型,因此您不需要 res.setHeader('Content-Type', 'text-html');但要么。 ;)

    来自expressjs.com

    res.render(view [, locals] [, callback]) 呈现视图并将呈现的 HTML 字符串发送到客户端。可选参数:

    locals,一个对象,其属性定义视图的局部变量。

    callback,回调函数。如果提供,该方法将返回可能的错误和呈现的字符串,但不执行自动响应。发生错误时,该方法在内部调用 next(err)。

    view 参数是一个字符串,它是要渲染的视图文件的文件路径。这可以是绝对路径,也可以是相对于视图设置的路径。如果路径不包含文件扩展名,则视图引擎设置确定文件扩展名。如果路径确实包含文件扩展名,那么 Express 将加载指定模板引擎的模块(通过 require())并使用加载模块的 __express 函数呈现它。

    【讨论】:

    • 所以,我删除了标题声明、send() 和模板文件的扩展名。我还将frontPage.html 重命名为frontPage.mu2,在view 的开头添加了/。整行现在显示为res.render('/frontPage', {ip: req.ip}); 不幸的是,现在它告诉我Failed to lookup view "/frontPage" in views directory "/Users/macuser/NodeJS/hello/views"
    • 你试过没有“/”吗? res.render('frontPage', {ip: req.ip});
    • 嗯...你在哪里安装小胡子?该错误似乎表明它无法解析寺庙或模板引擎。我会尝试在您的工作目录中运行 npm install mustache --save,或者添加 -g 选项以进行全局安装。 npm install -g mustache 看看是否可行。我很难重现您的错误。
    • 如果您喜欢 Mustache,请尝试使用车把...它在 Express 的 github 页面上的 hbs 下被列为开箱即用。
    猜你喜欢
    • 2018-01-10
    • 1970-01-01
    • 1970-01-01
    • 2022-08-06
    • 2022-10-17
    • 2018-10-04
    • 1970-01-01
    • 2016-06-19
    • 2022-12-11
    相关资源
    最近更新 更多