Setting up a server

The first file to be written must be a server file, so let's create a `server.js`. The minimal code required to kick-start a server with the hapi framework is as follows:

const hapi = require('hapi');
const server = new hapi.Server();
server.connection({
host: 'localhost',
port: 8000,
routes: { cors: true }
});
// Start the server
server.start((err) => {
if (err) {
throw err;
}
console.log('Server running at:', server.info.uri);
});

After reviewing the preceding code, we can observe that hapi starts its server by first configuring all the required data. It takes the host and port as input, and then finally starts the server. If we compare it with express, express needs a callback as an input first, and then the listening part.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.218.45.80