APITezx
Initialization
TezX is a high-performance, middleware-first server framework designed exclusively for the Bun runtime. It offers:
- Ultra-fast routing with dynamic path matching
- Middleware chaining at both global and route levels
- A clean, predictable request → context → response lifecycle
- Native integration with Bun’s HTTP server
Quick Start (server.ts)
// server.ts
import { TezX } from "tezx";
import { CustomRouter } from "./router.ts";
import { logger } from "tezx/middleware";
// Initialize TezX
const app = new TezX({
routeRegistry: new CustomRouter(), // Custom route tree
});
// Register global middleware
app.use(logger);
// Start server with Bun
Bun.serve({
port: 5000,
fetch: app.serve,
});Recommended Project Structure
project/
├── middlewares/
│ └── logger.ts
├── router.ts
├── server.ts
├── .env
└── src/
└── handlers/
└── home.tsTezX Configuration (TezXConfig)
| Option | Type | Description |
|---|---|---|
routeRegistry | Router | Custom router implementing the Router interface |
basePath | string | Global prefix applied to all routes |
Global Middleware
Middleware receives ctx and next():
import { Middleware } from "tezx";
export const loggerMiddleware: Middleware = async (ctx, next) => {
console.log(`[${ctx.method}] ${ctx.pathname}`);
await next();
};
// Register globally
app.use(loggerMiddleware);Route-Level Middleware
Attach middleware to a specific path:
app.when("ALL", "/admin/:section", adminAuthMiddleware);Custom 404 Handler
app.notFound((ctx) => ctx.status(404).text("🔍 Not Found"));Global Error Handling
app.onError((err, ctx) => {
console.error("Unhandled error:", err.details);
return ctx.status(500).text("🔥 Internal Server Error");
});Running the Server
bun run server.tsEnable auto-reload during development:
bun --watch run server.tsRuntime Compatibility (Bun Only)
| Feature | Bun |
|---|---|
.serve() | ✅ Native |
| Middleware | ✅ Yes |
| WebSocket | ✅ Built-in |
Summary
| Feature | Purpose |
|---|---|
TezX | Core application & runtime router |
use() | Register global middleware |
when() | Register dynamic route handlers |
notFound() | Custom 404 response handler |
onError() | Centralized error handling |
serve() | Integrates with Bun’s native HTTP server |
Integration Guide
Leverage TezX on the ultra-fast Bun runtime to build modern, scalable APIs with WebSocket support.
app.serve
The app.serve() method is the core handler for processing HTTP requests in TezX when running in Bun. It bridges Bun's server with your application logic, executing middleware and route handlers seamlessly.