Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I started working with Hapi nodejs framework. I am using "[email protected]" and here is my code in server.js to initiate application.

'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}`);
});

After running my project with node server.js from terminal it's throwing error as given below.

/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
like image 466
Jitendra Avatar asked Jan 22 '18 05:01

Jitendra


3 Answers

I found a solution to resolve my error. I just replaced
server.connection({ port: 3000, host: 'localhost' });
with
const server = new Hapi.Server({ port: 3000, host: 'localhost' });

Here is the description below: According to hapi v17.0.0 they Removed support for multiple connections for a single server

  1. The server.connection() method is replaced with options passed directly when creating a server object.
  2. connection property is removed from all objects.
  3. All connection methods moved to the server.
  4. Removed support for labels and select() methods and options.
like image 102
Jitendra Avatar answered Oct 19 '22 09:10

Jitendra


In hapi 16, there was support for server.connection(), but in hapi 17, they replaced server.connection() and instead

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

This can be used in node js.

If you are using typescript and typings, then

const server = new Hapi.server({port:3000 || process.env.port})
like image 1
Vikas Keskar Avatar answered Oct 19 '22 08:10

Vikas Keskar


replace with this

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

if you are using this

server.connection({   

    port: 8000,
    host: 'localhost'

});
like image 1
Jamil Noyda Avatar answered Oct 19 '22 08:10

Jamil Noyda