Member-only story
Server-Side Development with Hapi.js — Throw 400-Series Errors
3 min readFeb 21, 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.
Method Not Allowed Error
We can throw a method not allowed error with @hapi/boom
‘s methodNotAllowed
method.
For instance, we can write:
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.methodNotAllowed('that method is not allowed');
}
}); await server.start();
console.log('Server running at:', server.info.uri);
};
process.on('unhandledRejection', (err) => {
console.log(err);
process.exit(1);
});
init();
Then we get:
{"statusCode":405,"error":"Method Not Allowed","message":"that method is not allowed"}
as the response.
We can set the Allow
header with the 3rd argument.
To do this, we write:
const Hapi =…