Member-only story
Server-Side Development with Hapi.js — 400 Series Errors
3 min readFeb 23, 2021
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.
Conflict
We can return a 409 response ith @hapi/boom
with the Boom.conflict
method by writing:
const Hapi = require('@hapi/hapi');
const Boom = require('@hapi/boom');
const init = async () => {
const server = new Hapi.Server({
port: 3000,
host: '0.0.0.0'
});
server.route({
method: 'GET',
path: '/',
handler(request, h) {
throw Boom.conflict('conflict');
}
});
await server.start();
console.log('Server running at:', server.info.uri);
};
process.on('unhandledRejection', (err) => {
console.log(err);
process.exit(1);
});
init();
Then when we go to the /
route, we get:
{"statusCode":409,"error":"Conflict","message":"conflict"}
as the response.
Resource Gone Error
We can return the resource gone error by writing:
const Hapi = require('@hapi/hapi');
const Boom = require('@hapi/boom');
const init = async () => {
const server = new Hapi.Server({
port: 3000,
host: '0.0.0.0'
});
server.route({
method: 'GET'…