Modern Web Serving & Concurrency
Standard Web Fetch API (bee serve), node:http, and Lockless Multi-Isolate Thread Pools
Beejs offers two high-performance paradigms for building web services:
- Modern Standard Web Serving (
bee serve [file]): Built on W3C / WinterCG standardRequest/Responseandexport default { fetch(req) }model; - Node.js Compatible Serving (
bee run server.ts): Built onnode:httpbacked by a lockless multi-Worker thread pool in Rust Tokio.
1. Modern Web Application Serving (bee serve)
bee serve is the recommended, zero-overhead entrypoint for modern web applications, aligned with Cloudflare Workers, Deno, and Bun, featuring native TypeScript and JSX execution.
1.1 Writing Your First Web Service
Simply export an object with a fetch handler, or export the function directly:
// app.ts
export default {
async fetch(req: Request): Promise<Response> {
const url = new URL(req.url);
// Route matching
if (url.pathname === "/") {
return new Response("π Welcome to Beejs Web Server!");
}
if (url.pathname === "/api/echo" && req.method === "POST") {
const data = await req.json();
return new Response(JSON.stringify({ received: data, time: Date.now() }), {
status: 200,
headers: { "Content-Type": "application/json" }
});
}
if (url.pathname === "/api/info") {
return new Response(JSON.stringify({
runtime: "beejs",
version: "v1.16.0",
arch: process.arch,
platform: process.platform
}), {
headers: { "Content-Type": "application/json", "X-Powered-By": "beejs" }
});
}
return new Response("Not Found", { status: 404 });
}
};
1.2 Starting the Server
# Automatically detects and runs app.ts, app.js, server.ts, index.ts, etc.
$ bee serve
# Or specify a custom file, port, and host
$ bee serve app.ts --port 8080 --host 0.0.0.0
Console output:
π Starting Beejs Web Server on http://0.0.0.0:8080
π Serving application: app.ts
β
Listening on http://0.0.0.0:8080 (Ctrl+C to stop)
1.3 Key Highlights
- Zero Glue Overhead: Directly bridges incoming HTTP packets to standard
Requestinstances without unnecessary wrapper streams; - Async & Promise Support: Supports asynchronous handlers and drains microtasks automatically in the event loop;
- Full Body Mixin:
RequestandResponsefully implementreq.text(),req.json(), andreq.arrayBuffer(); - Sandbox Integration: Compatible with
--max-memory <MB>and--sandboxpermissions to bound untrusted execution.
2. Classic Node.js Compatible Serving (node:http)
For legacy services or Express-style architectures, node:http works out of the box:
// server.ts - Classic Node.js style
import http from 'node:http';
const server = http.createServer(async (req, res) => {
const { method, url } = req;
const parsedUrl = new URL(url || '/', `http://${req.headers.host}`);
res.setHeader('Content-Type', 'application/json; charset=utf-8');
res.setHeader('Access-Control-Allow-Origin', '*');
if (method === 'GET' && parsedUrl.pathname === '/api/users') {
res.writeHead(200);
res.end(JSON.stringify({ code: 0, data: ['Alice', 'Bob'] }));
return;
}
res.writeHead(404);
res.end(JSON.stringify({ error: 'Not Found' }));
});
server.listen(3000, () => {
console.log('π HTTP Server running: http://localhost:3000');
});
Run command:
bee run server.ts
3. Multi-Worker Thread Pool Architecture (--workers)
The Single-Thread Bottleneck
In conventional single-threaded runtimes, when a request triggers heavy JSON serialization, cryptography, or tensor inference, the event loop stalls and stalls all incoming requests.
Beejs Lockless Multi-Isolate Model
Beejs features a built-in multi-Worker thread pool in Rust:
Concurrent TCP Traffic
β
βΌ
+ββββββββββββββββββββββββββββββββββ+
β Main Dispatch Thread (Tokio)β
β - TCP Listener & SO_REUSEPORT β
β - Lockless Cross-Thread Queue β
+ββββββββββββββββββββββββββββββββββ+
β β β
βββββββββββββ β βββββββββββββ
βΌ βΌ βΌ
+βββββββββββββββββββ+ +βββββββββββββββββββ+ +βββββββββββββββββββ+
| Worker 1 (Isolate)| | Worker 2 (Isolate)| | Worker N (Isolate)|
| - Dedicated Heap | | - Dedicated Heap | | - Dedicated Heap |
| - Independent GC | | - Independent GC | | - Independent GC |
+βββββββββββββββββββ+ +βββββββββββββββββββ+ +βββββββββββββββββββ+
Launching Workers
Specify the number of worker isolates with -W or --workers:
bee run --workers 8 server.ts
Or via environment variable:
export BEE_WORKERS=8
bee run server.ts
Benefits:
- True Multi-Core Parallelism: Isolates execute independently in parallel on separate OS threads without blocking one another;
- Zero Startup Penalty: Workers are pre-warmed at boot time, eliminating thread creation overhead per request.
4. Production Benchmarks & Best Practices
Benchmark with tools like autocannon or wrk:
# Launch with 8 workers
bee run --workers 8 server.ts
# Benchmark 100 concurrent connections for 10 seconds
npx autocannon -c 100 -d 10 http://localhost:3000/api/users
Optimization Tips
- Prefer
bee servefor Microservices: The Fetch API model avoids EventEmitter and streaming buffer wrapper overhead, yielding higher RPS; - Calibrate Workers: For I/O services, set workers to
cores~2 * cores; for CPU/tensor-heavy workloads, match the physical core count; - Enforce Resource Quotas: For public-facing endpoints, combine with
--sandboxand--max-memory 512to prevent memory leaks and unauthorized disk access.