关于 hapi
面向使用 Hapi 构建服务应用的开发者的参考技能,涵盖服务器设置、路由、身份验证、验证规则、生命周期钩子、插件和 TypeScript 模式。
主要功能
- 01服务器设置与插件
- 02路由处理器和验证
- 03三层身份验证模式
- 04生命周期与扩展钩子
- 05TypeScript 路由和插件模式
适用场景
- 01启动 Hapi 服务器
- 02定义经过验证的路由
- 03配置路由身份验证
- 04用 server.inject() 测试请求
安装
复制下面的命令,在终端中运行。
$
npx skills add https:/ / github.com/ damusix/ skills --skill hapi安装后在 Claude Code 中输入/hapi确认已加载。
包含的文件
除 SKILL.md 外,安装时一起下载的参考资料和脚本。
.tessl-plugin/plugin.jsonreference/auth/basic.mdreference/file-serving/directory-handler.mdfile-handler.mdoverview.mdreference/jwt-auth/overview.mdtoken-api.mdvalidate.mdreference/lifecycle/boom.mdbounce.mdmethods.mdoverview.mdrequest-object.mdresponse-marshal.mdresponse-object.mdresponse-toolkit.mdreference/plugins/overview.mdreference/route/auth.mdcache-cors.mdhandler.mdmisc.mdoverview.mdpayload.mdpre.mdresponse.mdsecurity.mdvalidation.mdreference/server/auth.mdcache.mdcatbox-fs.md详细介绍
Hapi
快速入门
const server = Hapi.server({ port: 3000 });
server.route({ method: 'GET', path: '/', handler: () => 'ok' });
await server.start();
关键规则
- 通过装饰器和方法进行组合 - 通过装饰器公开服务,通过方法提供可复用逻辑
- 遵循生命周期 - 请求流程共 24 个步骤;参见生命周期概览
- 身份验证分为三层 - scheme → strategy → default;参见服务端身份验证
- 在路由中进行验证 - 在 params、query、payload 和 headers 上使用 joi schemas;参见验证
- 使用 Refs 为路由添加类型 - 使用
ServerRoute<Refs>,且只填写需要的键(Params、Query、Payload 等);省略的键保留默认值。参见路由框架
身份验证的三层模式:
server.auth.scheme('custom', schemeImpl); // 1. scheme(如何进行身份验证)
server.auth.strategy('session', 'custom', options); // 2. strategy(已配置的实例)
server.auth.default('session'); // 3. default(应用于所有路由)
Scheme authenticate 必须返回包含 credentials 和 artifacts 的 h.authenticated():
return h.authenticated({
credentials: { user: { id, name }, scope: ['user'] },
artifacts: { token } // 始终包含原始身份验证数据的 artifacts
});
路由验证模式:
server.route({
method: 'POST',
path: '/users',
options: {
validate: {
payload: Joi.object({
name: Joi.string().required(),
email: Joi.string().email().required()
})
}
},
handler: (request) => request.payload
});
工作流
- 创建服务端 - 构造函数选项参见服务端概览
- 注册插件 - 参见插件和插件结构
- 配置身份验证 - 参见身份验证 schemes和路由身份验证
- 验证身份验证 - 定义受保护的路由前,使用
server.inject()进行测试;参见网络 - 定义路由 - 参见路由概览和处理程序
- 添加扩展 - 参见生命周期钩子和预处理程序
Key Patterns
| Topic | Reference |
|---|---|
| Request/response objects | request, response |
| Response toolkit (h) | toolkit |
| Sessions (yar) | sessions |
| Caching & CORS | cache-cors, server cache, catbox-memory engine, catbox-fs engine, catbox-redis engine |
| Security headers | security |
| Payload parsing | payload |
| Decorations & methods | decorations, methods |
| MIME types (mimos) | mimos |
| Realms & plugin scoping | realm |
| Response marshalling | marshal pipeline |
| File serving (inert) | overview, file handler, directory handler |
| Basic authentication | basic auth |
| Error handling (Boom) | boom errors |
| Error filtering (Bounce) | bounce utility |
| WebSockets (nes) | overview, subscriptions, client |
| SSE (sse) | overview, api, subscriptions, session, replay |
| Startup & shutdown | startup lifecycle |
| Events | events |
| Testing (server.inject) | network |
| TypeScript overview | typescript |
| TypeScript auth typing | auth-scheme, type-author |
| JWT authentication | jwt overview, validate function, token API |
| TypeScript plugins | plugin-scaffold |
| Views & templates | vision overview, engines, context & layouts |
评论
支持 Markdown