HttpNext Pipeline
Copy-Paste Example
More Examples
Recommended layout:@hile/http file-route DSL. A Gateway fallback such as src/controllers/[namespace]/[...paths].controller.ts matches /-/blog/assets/logo.svg and exposes ctx.params.namespace === 'blog' plus ctx.params.paths === 'assets/logo.svg'. The safe [...paths] file name is retained by npm and works on Windows; do not use a literal *.controller.ts catch-all file.
Use loadModel() from pages or controllers for domain logic. If a Next.js page uses runtime model data, mark the route dynamic:
HttpNextProps composes the complete HttpProps contract. A deployment behind one trusted TLS-terminating reverse proxy can therefore configure the shared Koa/Next listener directly:
X-Forwarded-Proto: https, and be the only network path to the Hile listener. Hile middleware and controllers use the configured Koa proxy semantics; unmatched requests still reach Next.js as the original Node request with the same sanitized headers. This is transport configuration, not application policy.
Runtime And Lifecycle Notes
HttpNextkeeps its internalHttpinstance private and exposes onlyuse(),load(), andstart().- Except for
cwd, constructor options are theHttpPropspassed unchanged to the sharedHttpinstance, including complete Koa options, bounded reverse-proxy trust, and router options. - Hile middleware and controllers run first; unmatched requests are passed to Next.js with the original Node request and response.
@hile/http-next/request-signalis the Next-free App Router entrypoint for reading the current request’s cancellation signal.- Concurrent requests, multiple
HttpNextinstances, and compatible package copies in one JavaScript realm share one storage identity while retaining isolated asynchronous contexts. Separate PM2 processes, Workers, or VM realms intentionally keep independent request signals. - Development mode is determined by
process.env.NODE_ENV === 'development'. - Controllers use
{cwd}/src/controllersin development and{cwd}/dist/controllersin production with prefix/-. - Required catch-all controllers use the same
[...name]final-segment convention as@hile/http; unmatched requests alone continue to Next.js. load(directory)explicitly loads an additional controller directory with the same fixed conventions.- Call
use()andload()beforestart(); configuration is frozen once startup begins. - Next.js exclusively owns
public/,distDir,/_next/static, RSC, Server Functions, andnext.config. start(onReady)calls readiness only after Next is prepared and the shared HTTP server is listening.- The returned async stop function first asks the shared HTTP server to stop accepting new connections, then waits for HTTP drain and Next runtime cleanup concurrently. It tracks and terminates upgraded connections, including development HMR WebSockets, because Node’s HTTP drain does not own their protocol lifecycle. Development shutdown also closes disposable HTTP/compiler connections, matching Next’s own development-server policy; production HTTP requests retain graceful drain behavior.
- Normal stop attempts HTTP, Next, and startup-loaded controller cleanup even when one side fails. One cleanup failure is rethrown directly and multiple cleanup failures are reported together. Startup rollback also attempts every acquired cleanup, then preserves the original startup error.
Anti-Patterns
- Putting API routes in Next.js when the app is intentionally using Hile controllers.
- Importing the
@hile/http-nextroot from an App Router module; import request cancellation from@hile/http-next/request-signalinstead. - Calling
loadService()at module top level in Next.js files. - Serving Next.js
public/or/_next/staticthrough a separate Koa static middleware. - Trying to override
distDirat runtime instead of configuring it innext.config. - Forgetting
cwd; controller and Next project paths depend on it. - Treating
proxy: trueas a substitute for restricting direct listener access and sanitizing forwarded headers at the edge.
Verification Checklist
HttpNext.start()close function is registered withshutdown.- Controllers live under the conventional
src/controllersordist/controllersdirectory. - API routes use the fixed
/-prefix. - Next.js static assets and
public/are served by Next.js itself. - Next.js production build runs before
hile startin production. - App Router modules that read the request signal compile without Next.js’s
import-nextwarning. - Reverse-proxy settings describe the actual trusted hop topology and apply consistently to Koa and Next requests on the shared server.