Hapi.js is a small Node framework for developing back end web apps.
In this article, we’ll look at how to create back end apps with Hapi.js.
404 Handling
We can handle 404 errors easily with Hapi.
For example, we can write:
const Hapi = require('@hapi/hapi');
const Joi = require("@hapi/joi")
const init = async () => {
const server = Hapi.server({
port: 3000,
host: '0.0.0.0'
});
server.route({
method: '*',
path: '/{any*}',
handler (request, h) {
return '404 Error! Page Not Found!';
}
});
await server.start();
console.log('Server running on %s', server.info.uri);
};
process.on('unhandledRejection', (err) => {
console.log(err);
process.exit(1);
});
init();
We set the method
to '*'
to allow for all methods.
path
is set to ‘/{any*}’
to allow the route handler to watch for any path.
Server Methods
The server.method
method lets us add methods to our app.
Once we add the method, we can share them throughout the whole app.
For instance, we can write:
const Hapi = require('@hapi/hapi');
const add = (x, y) => {
return x + y;
};
const init = async () => {
const server = Hapi.server({
port: 3000,
host: '0.0.0.0'
});
server.method('add', add, {});
server.route({
method: 'GET',
path: '/',
handler(request, h) {
return server.methods.add(1, 2);
}
});
await server.start();
console.log('Server running on %s', server.info.uri);
};
process.on('unhandledRejection', (err) => {
console.log(err);
process.exit(1);
});
init();
We have the add
function that adds 2 numbers.
Then we call the server.method
method with it to register the method.
The first argument is the name. The 2nd argument is the function reference.
The 3rd argument is options, we can pass in.
Then we called it in the handler
with server.methods.add(1, 2);
.
So we get 3 as the response if we go to the /
route.
We can also write:
const Hapi = require('@hapi/hapi');
const add = (x, y) => {
return x + y;
};
const init = async () => {
const server = Hapi.server({
port: 3000,
host: '0.0.0.0'
});
server.method({
name: 'add',
method: add,
options: {}
});
server.route({
method: 'GET',
path: '/',
handler(request, h) {
return server.methods.add(1, 2);
}
});
await server.start();
console.log('Server running on %s', server.info.uri);
};
process.on('unhandledRejection', (err) => {
console.log(err);
process.exit(1);
});
init();
to do the same thing.
server.method
is called with an object with the same data but they are property values instead of arguments.
Also, we can namespace our methods with a namespace string as the first argument.
For instance, we can write:
const Hapi = require('@hapi/hapi');
const add = (x, y) => {
return x + y;
};
const init = async () => {
const server = Hapi.server({
port: 3000,
host: '0.0.0.0'
});
server.method('math.add', add);
server.route({
method: 'GET',
path: '/',
handler(request, h) {
return server.methods.math.add(1, 2);
}
});
await server.start();
console.log('Server running on %s', server.info.uri);
};
process.on('unhandledRejection', (err) => {
console.log(err);
process.exit(1);
});
init();
We register our math.add
method with:
server.method('math.add', add);
Then we call it with:
server.methods.math.add(1, 2)
Conclusion
We can handle 404 errors with a route and also add app-wide methods with server.methods
with Hapi.