Creating a configuration-based architecture

The configuration code pattern of hapi is followed everywhere, even for creating routes. Let's understand it by adding a simple GET method and its handler in the following snippet:

let phonebook = require('./phonebook');
module.exports = [{
method: 'GET',
path: '/phone/list',
config: {
handler(request, reply) {
reply({
message: "phonebook of superheroes",
data: phonebook.list
});
}
}
}]

The preceding snippet shows the minimal configuration required to create a route. It consists of the request method, which can be 'GET', 'POST', and so on; the URL path for URL navigation purposes; and a config property that consists of the request handler. This handler is used to write all sort of business logic a on request received.

Now, include the route file inside server.js and assign the routes to the hapi server before the server starts. So to wrap up, we have the following code in server.js:

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

Let's hit the route in the browser and view the response:

Similarly, we can add, update, and delete the entries from the phone book. Our routes.js will look as follows :

let phonebook = require('./phonebook');
module.exports = [{
method: 'GET',
path: '/phone/list',
config: {
handler(request, reply) {
reply({
message: "phonebook of superheroes",
data: phonebook.list
});
}
}
}, {
method: 'POST',
path: '/phone/add',
config: {
handler(request, reply) {
let payload = request.payload;
phonebook.list.unshift(payload);
reply({
message: "Added successfully",
data: phonebook.list
});
}
}
}, {
method: 'PUT',
path: '/phone/{phno}',
config: {
handler(request, reply) {
let payload = request.payload;
let phno = request.params.phno;
var notFound = [];
for (let i = phonebook.list.length - 1; i >= 0; i--) {
if (phonebook.list[i].phone_no == phno) {
phonebook.list[i].name = payload.name;
reply(phonebook.list);
return;
} else {
notFound.push(i);
}
}
if (notFound.length == phonebook.list.length) {
reply('not Found');
return;
}
}
}
}, {
method: 'DELETE',
path: '/phone/{phno}',
config: {
handler(request, reply) {
let phno = request.params.phno;
var notFound = [];
for (let i = phonebook.list.length - 1; i >= 0; i--) {
if (phonebook.list[i].phone_no == phno) {
phonebook.list.splice(i, 1);
reply({
message: "Delete successfully",
data: phonebook.list
});
return;
} else {
notFound.push(i);
}
}
if (notFound.length == phonebook.list.length) {
reply('not Found');
return;
}
}
}
}];
We need to use a browser extension to test the preceding REST APIs. POSTMAN is one of popular the extensions for REST API calls. Refer chapter 8 to learn in details about POSTMAN.

Woot! Our server APIs are ready. In the next chapter, we will consume these API calls by creating a frontend application.

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

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