Learn HAPI with Real Code Examples
Updated Nov 27, 2025
Code Sample Descriptions
1
Hapi.js Simple REST API
const Hapi = require('@hapi/hapi');
const todos = [];
const init = async () => {
const server = Hapi.server({
port: 3000,
host: 'localhost'
});
server.route({
method: 'GET',
path: '/todos',
handler: (request, h) => todos
});
server.route({
method: 'POST',
path: '/todos',
handler: (request, h) => {
const todo = request.payload;
todos.push(todo);
return h.response(todo).code(201);
}
});
await server.start();
console.log('Server running on %s', server.info.uri);
};
init();
Demonstrates a simple Hapi.js server with routes for listing and creating Todo items.