Static File
Serve static files (HTML, CSS, JS, images, JSON, etc.) in Bun using TezX’s serveStatic.
Node.js support requires built-in modules like fs and path.
Supported Platform: tezx/static (works in Bun natively; Node.js requires fs + path)
Import
import { serveStatic } from "tezx/static";Note for Node.js:
serveStaticrelies onfsandpathinternally, so make sure these core modules are available.
Usage
import { TezX } from "tezx";
import { serveStatic } from "tezx/static";
const app = new TezX();
app.static(serveStatic('/', './public', {
extensions: ['html', 'css', 'js'],
cacheControl: 'max-age=86400',
headers: { 'X-Static-Served-By': 'TezX-Bun' }
}));Types
type StaticFileArray = {
fileSource: string; // Absolute or relative path on disk
route: string; // Public URL path
}[];
type StaticServeOption = {
cacheControl?: string; // Cache-Control header
headers?: Record<string, string>; // Custom headers
extensions?: string[]; // Allowed file extensions
};
type ServeStatic = {
files: StaticFileArray;
options?: StaticServeOption;
};Folder Example
my-app/
├── public/
│ ├── index.html
│ ├── style.css
│ └── script.js
└── server.tsKey Features
| Feature | Description |
|---|---|
| Extension filter | Serve only allowed file types |
| Route mapping | Map files to custom URL paths |
| Headers | Add custom headers like Cache-Control, ETag |
| Performance | Fast static file serving |
Tips for Bun
- Paths can be relative;
serveStaticresolves them internally. - Always filter extensions to avoid exposing sensitive files.
- Combine with dynamic routes for hybrid apps.
- Use caching headers for optimal performance.
💡 Reference: MDN – Cache-Control Header
JWT Utility
Provides JWT signing and verification using HMAC (HS256 or HS512) for secure token-based authentication Bun environments (with Web Crypto API).
useFormData
useFormData is a powerful helper designed for efficient parsing, validation, and management of multipart/form-data HTTP requests in TezX applications. It simplifies handling complex file uploads combined with form fields while enforcing strict limits to protect your server from overload and security risks.