gpt4 book ai didi

javascript - TypeError : server. View 不是 hapi.js 中的函数

转载 作者:行者123 更新时间:2023-12-02 14:11:48 24 4
gpt4 key购买 nike

我是 Node.js 的初学者,我正在为我的 Web 应用程序使用 hapi 框架。我使用 Handlebars 作为模板。当我配置服务器 View 时,我收到类型错误。

    'use strict';

const hapi = require('hapi');
const server = new hapi.Server();
const inert = new require('inert');

server.connection({
host: '127.0.0.1',
port: 8080,
});

//Starting server
server.start((error) => {
if(error){
throw error;
}
console.log("Server running" + server);
});

server.register(inert, () => {
console.log("hhh");
server.views({
engines: {
html: require('handlebars')
},
path: 'views',
layoutPath: 'views/layout',
layout: 'default',
partialsPath: 'views/partials'
//helpersPath: 'views/helpers',
});
});

我收到此错误:

    TypeError: server.views is not a function
at server.register (/home/developer/Workspace/kravein-test/app/backend/server.js:22:9)
at process.nextTick (/home/developer/Workspace/kravein-test/node_modules/hoek/lib/index.js:854:22)
at _combinedTickCallback (internal/process/next_tick.js:67:7)
at process._tickCallback (internal/process/next_tick.js:98:9)
at Module.runMain (module.js:577:11)
at run (bootstrap_node.js:352:7)
at startup (bootstrap_node.js:144:9)
at bootstrap_node.js:467:3

我使用的是 hapi 15.0.3。谢谢

最佳答案

要使用server.views,您需要 Vision plugin

例如带 Handlebars

const server = new Hapi.Server();
server.connection({ port: 8000 });

const handler = function (request, reply) {

reply.view('basic/index', {
title: 'examples/views/handlebars/basic.js | Hapi ' + request.server.version,
message: 'Hello World!'
});
};

server.register(require('vision'), (err) => {

if (err) {
throw err;
}

server.views({
engines: { html: require('handlebars') },
path: __dirname + '/templates'
});

server.route({ method: 'GET', path: '/', handler: handler });
});

关于javascript - TypeError : server. View 不是 hapi.js 中的函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39478689/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com