1. Home
  2. /
  3. Hapi
  4. /
  5. Hapi.js Simple REST API

Hapi.js Simple REST API - Hapi Typing CST Test

Loading…

Hapi.js Simple REST API — Hapi Code

Demonstrates a simple Hapi.js server with routes for listing and creating Todo items.

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();

Hapi Language Guide

Hapi is a rich, flexible web framework for Node.js designed to build scalable and maintainable applications and services with configuration-driven architecture and powerful plugin system.

Primary Use Cases

  • ▸REST APIs and GraphQL endpoints
  • ▸Enterprise web applications
  • ▸Microservices with plugin-based modularity
  • ▸Secure backends with authentication/authorization
  • ▸Server-side rendering and templating

Notable Features

  • ▸Configuration-driven routing and plugins
  • ▸Built-in input validation with Joi
  • ▸Caching and state management support
  • ▸Authentication strategies and security plugins
  • ▸Extensible plugin system for modular apps

Origin & Creator

Created by Eran Hammer in 2011 and maintained by the Hapi community.

Industrial Note

Hapi is widely used in large-scale enterprise applications, APIs, and microservices where configuration consistency, security, and maintainability are priorities.

Practice Other Languages

CReactPythonC++RustTypeScriptKotlinPHPJavaC#RubyMqlCqlN1qlCypher