Catalog / Hapi.js Cheat Sheet
Hapi.js Cheat Sheet
A comprehensive cheat sheet for Hapi.js, covering essential concepts, configuration, routing, plugins, and more, to help you build robust and scalable web applications.
Core Concepts & Setup
Server Initialization
Creating a basic Hapi.js server instance.
|
Explanation:
|
Basic Routing
Defining a simple route to handle GET requests.
|
Explanation:
|
Route Configuration Options
|
HTTP method (GET, POST, PUT, DELETE, etc.). |
|
URL path for the route. |
|
Function to process the request and return a response. |
|
Object for route-specific configurations (e.g., validation, authentication). |
Request Handling & Validation
Accessing Request Data
Accessing data from various parts of the request object.
|
Payload Validation with Joi
Using Joi for request payload validation.
|
Explanation:
|
Query Parameter Validation
|
Validates query parameters. |
|
Validates route parameters. |
|
Defines what happens when validation fails (e.g., ‘log’, ‘error’). |
Plugins & Decorators
Registering Plugins
Registering a plugin to extend server functionality.
|
Server Decorators
Adding custom methods to the server object.
|
Request Decorators
|
Adds a method to the request object. |
|
Adds a method to the response toolkit. |
|
Adds a method to the server object. |
Advanced Features
Authentication Strategies
Implementing authentication strategies using plugins like
|
Caching Strategies
Configuring caching strategies to improve performance.
|
Middleware and Lifecycle Methods
|
Runs before the handler function. |
|
Runs after the handler function, before sending the response. |
|
Runs before sending the response, allowing modification. |