【问题标题】:Can't add templating engine in plugin HapiJS无法在插件 HapiJS 中添加模板引擎
【发布时间】:2015-10-16 06:18:10
【问题描述】:

我想为每个插件实现路由,但我无法在插件中添加视图引擎。我已经看到了可行的示例,例如:https://github.com/hapijs-edge/hapi-plugins.com/blob/master/lib/routes.js,但我收到了一个错误,提示 server.views is not a function

var Hapi = require('hapi');

var server = new Hapi.Server();
server.connection();

var myPlugin = {
    register: function (server, options, next) {

        // Error happens here, should be able to see server.views()
        console.log(server.views()); 
        next();
    }
};

myPlugin.register.attributes = {
    name: 'myPlugin',
    version: '1.0.0'
};

server.register( myPlugin, function(err) {

    if (err) {
        console.error('Failed to load a plugin:', err);
    }
} );

server.start(function () {

    console.log('Server running at:', server.info.uri);
});

【问题讨论】:

    标签: javascript node.js hapijs templating-engine


    【解决方案1】:

    好像是hapi v10的问题。试试“npm i hapi@8.8.1”,那个版本应该可以工作

    【讨论】:

    • 您能否说明如何或为什么要提高此答案的质量和价值?
    【解决方案2】:

    hapi 的人给我指路了……从 hapi 9 开始,需要视觉模块来装饰服务器并可以访问 views 方法。现在可以正常使用了!

    【讨论】:

      【解决方案3】:

      从 hapi => 9.x.x 开始,您需要先注册 vision plugin 才能使用 server.view 功能。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-03-14
        • 2013-12-20
        • 2018-10-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多