Introduction
Hapi is a rich framework for building applications and services in Node.js. It favors configuration over code and provides a powerful plugin system that makes it straightforward to isolate business logic into reusable, testable modules.
What Hapi Does
- Handles HTTP request routing with path parameters, query validation, and payload parsing
- Validates input and output automatically using Joi schema definitions on route configs
- Manages server lifecycle events including startup, shutdown, and request extensions
- Supports plugins that encapsulate routes, methods, and decorations as composable units
- Provides built-in caching strategies with support for catbox adapters (Redis, Memcached, etc.)
Architecture Overview
Hapi processes requests through a well-defined lifecycle: onRequest, onPreAuth, authentication, authorization, onPreHandler, handler, onPostHandler, and onPreResponse. Each extension point lets plugins and application code intercept and modify the request or response. The server object is a singleton that manages connections, routes, and the plugin dependency graph. Authentication is handled through named strategies that you register and assign to routes declaratively.
Self-Hosting & Configuration
- Install with
npm install @hapi/hapiand create a server instance with host and port config - Register plugins using
server.register()with automatic dependency resolution - Define routes with built-in validation by adding Joi schemas for params, query, and payload
- Configure TLS by passing key and cert options to the server constructor
- Use
server.ext()to hook into request lifecycle events for cross-cutting concerns
Key Features
- Configuration-driven routing where validation, auth, and caching are declared on the route object
- Plugin architecture that enables splitting large applications into isolated, reusable modules
- Built-in authentication framework with strategy registration and scheme definition
- Automatic API documentation generation through plugins like hapi-swagger
- Comprehensive logging through the server events system and the good/hapi-pino reporters
Comparison with Similar Tools
- Express — minimal and callback-based; Hapi provides structured configuration, built-in validation, and a plugin system out of the box
- Fastify — focuses on raw speed with schema-based validation; Hapi prioritizes configuration clarity and enterprise patterns
- Koa — middleware-only approach using async/await; Hapi offers a richer lifecycle model with named extension points
- NestJS — decorator-heavy DI framework; Hapi uses a simpler plugin-based composition model
- AdonisJS — full MVC framework with ORM; Hapi is more focused on the HTTP layer and leaves data access to you
FAQ
Q: Is Hapi still actively maintained? A: Yes. Hapi continues to receive updates and security patches. The core team releases under the @hapi npm scope.
Q: How does Hapi handle input validation? A: Routes accept Joi schemas for params, query, payload, and headers. Invalid requests are automatically rejected with descriptive error responses before reaching the handler.
Q: Can Hapi scale for high-traffic applications? A: Yes. Hapi was originally built at Walmart Labs to handle Black Friday traffic on the retail site, processing hundreds of millions of requests.
Q: How does the plugin system work? A: Plugins are objects with a register function that receives the server instance. They can add routes, decorate the server or request objects, and declare dependencies on other plugins.