API Reference

Hapi

Configuration-centric Node.js framework with built-in validation, authentication strategies, caching, and a mature plugin ecosystem.

Official Documentation ↗
CompatibilityThis documentation covers version 21.4.12. Check the official docs for the latest API surface.

Installation

npmnpm install @hapi/hapi joi
pnpmpnpm add @hapi/hapi joi
yarnyarn add @hapi/hapi joi

Methods

Hapi is a Node.js framework where configuration carries most of the behavior. Routes declare validation, authentication, caching, and response shaping as options, and plugins extend the server through a well-defined registration model instead of ad hoc middleware ordering.

Overview

A Hapi server is created, configured, and started explicitly. Routes are objects, not chained callbacks, which makes input validation and auth policies visible next to the handler. The request lifecycle exposes extension points such as onRequest and onPreHandler, and the response toolkit h builds typed responses with status codes and headers.

Installation

Install @hapi/hapi together with joi for validation. Additional capabilities come from plugins, such as @hapi/inert for static files, @hapi/vision for templates, and hapi-auth-jwt2 for JWT authentication. TypeScript definitions are published as @types/hapi__hapi. Hapi is CommonJS-first, so require imports are the conventional choice.

Core API walkthrough

Hapi.server creates the instance with host and port. server.route registers a method, path, and handler. route.options.validate inspects params, query, and payload with Joi and rejects invalid input with a 400. server.register loads plugins that decorate the server or request, and server.inject runs a request in-process for tests.

Pitfalls

Route validation happens before the handler, so malformed payloads never reach business logic. Plugin registration is asynchronous and must be awaited before routes that depend on decorations are added. Handlers that return undefined produce an empty 200 rather than an error. Also avoid reading raw payload streams after validation has already consumed them.

Verification

Use server.inject to assert status codes, validation failures, and auth rejections without a port. Start the server and check the printed info.uri responds with curl, and confirm a missing required field returns 400 with a Joi message rather than a 500.

Errors

The following points are quoted verbatim from the official documentation. 来源:https://hapi.dev/ · 取证方式:HTTP GET 官方 README/文档页(HTML 去标签) · 抓取时间:2026-11-07T13:50:11.224Z. Any error type, throw condition or recovery step not stated there is left unspecified; this page does not infer exception classes or return values.

  • Along with protecting server load with payload limits and request timeouts, hapi blocks error messages that could leak information or echo back exploits. (https://hapi.dev/)

Security notes

Quoted verbatim from the official documentation; this is not a complete threat model. 来源:https://hapi.dev/ · 取证方式:HTTP GET 官方 README/文档页(HTML 去标签) · 抓取时间:2026-11-07T13:50:11.224Z.

  • Trusted in production by teams at Why teams pick hapi Security Zero external dependencies. (https://hapi.dev/)
  • Support Reliable ownership and a strict, well defined security protocol. (https://hapi.dev/)
  • Advanced Features — with encrypted and signed cookies, secret or key rotation, and HTTP security headers, there are no excuses for building insecure applications. (https://hapi.dev/)

FAQ

Is hapi still maintained?
Yes. @hapi/hapi is the scoped package for current releases, and hapi remains actively maintained with a stable v21 line. The old unscoped hapi package is deprecated.
How do I test routes without binding a port?
Call await server.initialize() and use server.inject with method, url, headers, and payload. The response exposes statusCode, headers, and a parsed result, mirroring a real request.
How do I add authentication?
Register an auth scheme plugin such as hapi-auth-jwt2, configure a strategy with server.auth.strategy, and set auth on routes or server.auth.default to require credentials.