TypeError: server.connection is not a function in Hapi nodejs

TypeError: server.connection is not a function in Hapi nodejs

我开始使用 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() 方法和选项的支持。

在 hapi 16 中,支持 server.connection(),但在 hapi 17 中,他们替换了 server.connection() 而不是

const Hapi = require('hapi')
const server = Hapi.server({
port:3000 || process.env.port
})

这个可以在node js中使用

如果您使用的是打字稿和打字,那么

const server = new Hapi.server({port:3000 || process.env.port})

替换为这个

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

如果您正在使用这个

server.connection({   

    port: 8000,
    host: 'localhost'

});