@hile/http
Build Koa HTTP APIs with file-system controllers, middleware, response plugins, and Zod validation.Choose This Package When
Use When
Use@hile/http for Koa-based APIs, middleware, manual routes, file-system controllers, Zod validation, and response plugins.
Do Not Use When
- Do not use it for Next.js pages; use
@hile/http-nextwhen Next.js and API routes share a port. - Do not assume Zod validation mutates/coerces Koa context data. It validates only.
Install
Imports
Copy-Paste Example
More Examples
safeParse().data back to ctx.request.body.
Trusted reverse proxy
Http does not trust forwarded headers by default. When the listener is reachable only through a controlled reverse proxy, enable Koa’s proxy semantics explicitly and bound the number of proxy IPs that may be read:
X-Forwarded-Proto and the configured client-IP header. It must send X-Forwarded-Proto: https after TLS termination so ctx.protocol, ctx.secure, and Secure Cookie handling reflect the public connection. Keep the Hile listener inaccessible to untrusted direct clients: proxy: true trusts these headers and is not itself a network trust boundary.
Use proxyIpHeader only when the deployment uses a client-IP header other than X-Forwarded-For. maxIpsCount bounds the proxy chain read from that header; set it to the known number of trusted hops instead of relying on Koa’s unbounded default.
HttpProps composes Koa’s complete constructor options with Hile’s port and find-my-way options. Koa settings such as env, subdomainOffset, keys, asyncLocalStorage, proxy, proxyIpHeader, and maxIpsCount are passed through without a Hile-owned mirror or a second Hile-specific option contract.
Runtime And Lifecycle Notes
new Http({ port })creates a Koa app from the completeHttpPropsobject and afind-my-wayrouter.proxydefaults tofalse. When enabled,proxyIpHeaderandmaxIpsCountare passed to Koa before any middleware or controller handles requests.http.use(middleware)registers Koa middleware beforelisten().http.listen()returns a close function.http.load(directory, options)scans*.controller.{ts,js,tsx,jsx,mjs}by suffix.defineController()supports method-only, method-plus-middlewares, and metadata-plus-Zod forms.- Response plugins transform handler return values and set
ctx.bodywhen the final result is notundefined.
Anti-Patterns
- Setting
ctx.bodyand returning a value from the same controller. - Loading controllers after starting only because an old example does it; prefer load before listen in new code.
- Using old validation examples that assume Zod coercion rewrote
ctx.query. - Enabling
proxyon a listener that untrusted clients can reach directly, or allowing an edge proxy to preserve client-supplied forwarded headers.
Verification Checklist
- Controller files default-export
defineController(...)or an array of controllers. - Controllers return response values.
- Boot service awaits
http.load()beforehttp.listen(). - Zod schemas are used for validation, and parsed data is explicitly parsed when needed.
- Reverse-proxy deployments replace forwarded headers, protect the direct listener, and set
maxIpsCountto the known proxy-hop count.
Related Recipes
HTTP API + Model + TypeORM
Complete Example
Controller:Package-Local AI Guide
This package also shipsAI.md in npm so agents can read accurate examples after installation.