Member-only story

Server-Side Development with Hapi.js — Routing

John Au-Yeung
3 min readFeb 20, 2021

--

Photo by Andreas Weiland on Unsplash

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.

Routing

We can add routes easily to our Hapi app.

For example, we can write:

const Hapi = require('@hapi/hapi');const init = async () => {
const server = Hapi.server({
port: 3000,
host: '0.0.0.0',
});
server.route({
method: 'GET',
path: '/',
handler (request, h) {
return 'Hello World!';
}
});

await server.start();
console.log('Server running on %s', server.info.uri);
};
process.on('unhandledRejection', (err) => {
console.log(err);
process.exit(1);
});
init();

to add a simple route with:

server.route({
method: 'GET',
path: '/',
handler(request, h) {
return 'Hello World!';
}
});

The method has the request method the route accepts.

path has the route path.

handler has the route handler to handle the request.

request has the request data.

--

--

No responses yet