【问题标题】:In Hapi.js server is not starting在 Hapi.js 服务器未启动
【发布时间】:2020-02-18 13:58:17
【问题描述】:
const Hapi=require('hapi');

//Init server
const server=new Hapi.Server();

//Add connection
server.connection({
    port:3000,
    host:'localhost'
});

//Home route

server.route({
    method:'GET',
    path:'/',
    handler:(request,reply)=>{
        reply('Hello World');
}
})



// Start Server
server.start((err) => {
    if(err){
        throw err;
    }

    console.log(`Server started at: ${server.info.uri}`);
});

块引用 这是我第一个在主页上打印 hello world 的 hapi.js 服务器,但显示 server.connection 不是一个函数,而且处理程序也没有前途。 请帮帮我。

【问题讨论】:

标签: hapijs


【解决方案1】:

试试这个

const Hapi=require('hapi');
//Init server
const server = new Hapi.Server({ port: 3000, host: 'localhost' });
//Home route

server.route({
  method: 'GET',
  path: '/',
  handler: function (request, h) {
  return 'hello world';
    }
  });

// Start Server
server.start(err => {
    if (err) {
        // Fancy error handling here
        console.error(err);
        throw err;
    }
    console.log(`Server started at ${ server.info.uri }`);
});

Hapi v17.0.0^ 不支持单个服务器的多个连接,不再将回复函数作为第二个参数传递

【讨论】:

    【解决方案2】:

    检查你的 node.js 版本旧版本不支持最新的 hapi 格式。 hapi.js 的旧版本和新版本之间似乎有很多差异

    const Hapi = require('@hapi/hapi');
    
    const port = process.env.PORT || 3000;
    
    const init = async () => {
    
    const server = Hapi.server({
        port: port,
        host: 'localhost'
    });
    
    server.route({
        method: 'GET',
        path: '/',
        handler: (request, h) => {
            return 'Hello World!';
        }
    });
    
    await server.start();
    console.log('Server running on %s', server.info.uri);
    };
    
    
    init();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-07-26
      • 2015-12-30
      • 2017-03-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多