Reputation: 139
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}`);
});
Blockquote This is my first hapi.js server for printing hello world in home page but shows server.connection is not a function and also handlers are not promising. Plz help me.
Upvotes: 1
Views: 1685
Reputation: 277
check you node.js version older versions does not support the latest hapi format. there seems to be alot of diiference betwwen older versions and newer versions of 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();
Upvotes: 1
Reputation: 203
Try this
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^ is not supporting for multiple connections for a single server and no longer passing the reply function as the second argument
Upvotes: 2