Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/thanhhoajs/thanhhoa
ThanhHoa is a lightweight, high-performance web framework for Bun, designed to make server-side development simple and enjoyable.
https://github.com/thanhhoajs/thanhhoa
bun http server thanhhoa thanhhoajs
Last synced: 18 days ago
JSON representation
ThanhHoa is a lightweight, high-performance web framework for Bun, designed to make server-side development simple and enjoyable.
- Host: GitHub
- URL: https://github.com/thanhhoajs/thanhhoa
- Owner: thanhhoajs
- License: mit
- Created: 2024-06-11T09:09:52.000Z (7 months ago)
- Default Branch: main
- Last Pushed: 2024-11-29T15:56:25.000Z (26 days ago)
- Last Synced: 2024-11-29T16:14:47.623Z (26 days ago)
- Topics: bun, http, server, thanhhoa, thanhhoajs
- Language: TypeScript
- Homepage: https://www.npmjs.com/package/@thanhhoajs/thanhhoa
- Size: 79.1 KB
- Stars: 15
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# @thanhhoajs/thanhhoa
ThanhHoa is a high-performance, lightweight web framework for Bun, crafted to simplify server-side development while delivering maximum speed.
## Features
- ๐ **Built for Speed**: Utilizes Bun's non-blocking I/O for ultra-fast request processing.
- ๐งฉ **Modular Design**: Simple and flexible middleware system.
- ๐ฃ๏ธ **Intuitive Routing**: Supports parameters and dynamic paths.
- ๐๏ธ **Full HTTP Support**: Handles GET, POST, PUT, PATCH, and DELETE methods.
- ๐ **Standardized Error Handling**: Comes with `HttpException` for structured error responses.
- ๐ญ **TypeScript Support**: Fully typed for a streamlined developer experience.
- ๐๏ธ **Built-in Caching**: URL caching for optimized performance.
- โฑ๏ธ **Request Timeout**: Configurable timeout for managing long-running requests.
- ๐งน **Automatic Cache Cleanup**: Regular cleanup of stale cache entries.
- ๐ **CORS Middleware**: Flexible configuration for CORS and security headers.
- ๐ก๏ธ **Helmet Middleware**: Enhanced HTTP security headers.
- ๐ **Rate Limiting**: Middleware for managing request rates from clients.
- ๐๏ธ **Response Compression**: Gzip compression middleware to reduce response size.
- ๐๏ธ **Custom Static Directories**: Supports multiple static directories for organized file management.## Installation
Install ThanhHoa with Bun:
```bash
bun add @thanhhoajs/thanhhoa
```## Quick Start
Hereโs a quick setup to get started with ThanhHoa:
```typescript
import { ThanhHoa, type IRequestContext } from '@thanhhoajs/thanhhoa';const app = new ThanhHoa();
app.get('/', (ctx: IRequestContext) => {
return new Response('Hello, ThanhHoa!', {
headers: { 'Content-Type': 'text/plain' },
});
});app.listen({ port: 3000 });
```Run your app:
```bash
bun run app.ts
```Visit `http://localhost:3000` to see "Hello, ThanhHoa!" in your browser.
## Routing
ThanhHoa offers flexible routing with support for dynamic parameters:
```typescript
app.get('/user/:id', (ctx: IRequestContext) => {
return new Response(`User ID: ${ctx.params.id}`);
});app.post('/user', async (ctx: IRequestContext) => {
const body = await ctx.request.json();
// Process the body...
return new Response('User created', { status: 201 });
});
```## Middleware
ThanhHoa allows you to add middleware globally or for specific routes:
```typescript
// Custom middleware
const logger = async (ctx: IRequestContext, next: INextFunction) => {
console.log(`${ctx.request.method} ${ctx.request.url}`);
return next();
};app.use(logger);
app.use(corsMiddleware());
app.use(helmetMiddleware());
app.use(rateLimiter({...}));
app.use(cacheMiddleware());
app.use(compression({...}));app.get('/protected', authMiddleware, (ctx: IRequestContext) => {
return new Response('Protected route');
});
```## Static Directory Support
Easily serve static files from multiple directories:
```typescript
const app = new ThanhHoa();app.listen({
port: 3000,
staticDirectories: [
{
path: '/images',
directory: 'public/images',
},
{
path: '/assets',
directory: 'public/assets',
},
],
});
```## Error Handling
Built-in error handling using `HttpException`:
```typescript
app.get('/error', () => {
throw new HttpException('Something went wrong', 500);
});
```## Performance Benchmark
**Handling 10,000 concurrent requests:**
- **Average Latency**: 1.03ms
- **Memory Usage**: 0.01 MBThe **ThanhHoa framework** shines with sub-2ms response times and minimal memory usage, making it perfect for high-throughput applications.
_Setup_: Simple GET route (`/test`) over 5,000 iterations, 2 requests per iterationโshowcasing its stability and lightweight nature. ๐โจ
## Author
Nguyen Nhu Khanh
## License
[MIT License](https://github.com/thanhhoajs/thanhhoa?tab=MIT-1-ov-file)