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
I found a solution to resolve my error. I just replacedserver.connection({ port: 3000, host: 'localhost' });
withconst 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
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})
replace with this
const server = new Hapi.Server({
host: 'localhost',
port: 3000
})
if you are using this
server.connection({
port: 8000,
host: 'localhost'
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With