gpt4 book ai didi

node.js - TypeError : server. 连接不是 Hapi nodejs 中的函数

转载 作者:IT老高 更新时间:2023-10-28 23:19:40 35 4
gpt4 key购买 nike

我开始使用 Hapi nodejs 框架。我正在使用“hapi@17.2.0”,这是我在 server.js 中启动应用程序的代码。

'use strict';

const Hapi = require('hapi');

const server = new Hapi.Server();
server.connection({ port: 3000, host: 'localhost' });

server.route({
method: 'GET',
path: '/',
handler: function (request, reply) {
reply('Hello, world!');
}
});

server.start((err) => {

if (err) {
throw err;
}
console.log(`Server running at: ${server.info.uri}`);
});

使用 node server.js 从终端运行我的项目后,它会抛出如下所示的错误。

/var/www/html/hello_hapi/server.js:6
server.connection({ port: 3000, host: 'localhost' });
^

TypeError: server.connection is not a function
at Object.<anonymous> (/var/www/html/hello_hapi/server.js:6:8)
at Module._compile (module.js:612:30)
at Object.Module._extensions..js (module.js:623:10)
at Module.load (module.js:531:32)
at tryModuleLoad (module.js:494:12)
at Function.Module._load (module.js:486:3)
at Function.Module.runMain (module.js:653:10)
at startup (bootstrap_node.js:187:16)
at bootstrap_node.js:608:3

最佳答案

我找到了解决我的错误的解决方案。我刚换了
server.connection({ port: 3000, host: 'localhost' });

const server = new Hapi.Server({ port: 3000, host: 'localhost' });

下面是描述:根据 hapi v17.0.0,他们删除了对单个服务器的多个连接的支持

  1. server.connection() 方法被替换为创建服务器对象时直接传递的选项。
  2. 从所有对象中删除连接属性。
  3. 将所有连接方法移至服务器。
  4. 删除了对标签和 select() 方法和选项的支持。

关于node.js - TypeError : server. 连接不是 Hapi nodejs 中的函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48375126/

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