Custom Servers
Use AzuraJS with Bun, Deno, Cloudflare Workers, and more
Custom Servers π
AzuraJS can be used with any server that supports the Web Fetch API, just like Hono! This gives you flexibility to deploy on Bun, Deno, Cloudflare Workers, Vercel Edge Functions, and more.
Using with Bun π
Bun provides a fast native HTTP server. Use AzuraJS with Bun.serve:
TypeScript
import { AzuraClient } from "azurajs";
const app = new AzuraClient();
app.get("/", (req, res) => {
res.json({ message: "Hello from AzuraJS on Bun!" });
});
app.get("/user/:id", (req, res) => {
res.json({
userId: req.params.id,
runtime: "Bun"
});
});
app.post("/data", (req, res) => {
res.json({
received: req.body,
timestamp: Date.now()
});
});
// Use with Bun's native server
const server = Bun.serve({
port: process.env.PORT || 3000,
fetch: app.fetch.bind(app),
});
console.log(`π Server running on http://localhost:${server.port}`);JavaScript
const { AzuraClient } = require("azurajs");
const app = new AzuraClient();
app.get("/", (req, res) => {
res.json({ message: "Hello from AzuraJS on Bun!" });
});
app.get("/user/:id", (req, res) => {
res.json({
userId: req.params.id,
runtime: "Bun"
});
});
app.post("/data", (req, res) => {
res.json({
received: req.body,
timestamp: Date.now()
});
});
// Use with Bun's native server
const server = Bun.serve({
port: process.env.PORT || 3000,
fetch: app.fetch.bind(app),
});
console.log(`π Server running on http://localhost:${server.port}`);Run it:
bun run bun-server.ts # or .jsWhy Use Bun?
- β‘ 3x faster than Node.js
- π Native TypeScript support
- π¦ Built-in bundler and test runner
- π₯ Hot reload out of the box
Using with Deno π¦
Deno is a secure runtime for JavaScript and TypeScript. Use AzuraJS with Deno.serve:
TypeScript
import { AzuraClient } from "azurajs";
const app = new AzuraClient();
app.get("/", (req, res) => {
res.json({ message: "Hello from AzuraJS on Deno!" });
});
app.get("/api/:name", (req, res) => {
res.json({
greeting: `Hello ${req.params.name}!`,
runtime: "Deno"
});
});
// Use with Deno.serve
Deno.serve({ port: 3000 }, app.fetch.bind(app));
console.log("π¦ Server running on http://localhost:3000");JavaScript
const { AzuraClient } = require("azurajs");
const app = new AzuraClient();
app.get("/", (req, res) => {
res.json({ message: "Hello from AzuraJS on Deno!" });
});
app.get("/api/:name", (req, res) => {
res.json({
greeting: `Hello ${req.params.name}!`,
runtime: "Deno"
});
});
// Use with Deno.serve
Deno.serve({ port: 3000 }, app.fetch.bind(app));
console.log("π¦ Server running on http://localhost:3000");Run it:
deno run --allow-net --allow-read deno-server.ts # or .jsWhy Use Deno?
- π Secure by default - Explicit permissions required
- π¦ No package.json - Direct URL imports
- π Web standards - Compatible APIs
- β‘ Fast startup - Optimized runtime
Using with Cloudflare Workers βοΈ
Deploy globally on Cloudflare's edge network:
TypeScript
import { AzuraClient } from "azurajs";
const app = new AzuraClient();
app.get("/", (req, res) => {
res.json({
message: "Hello from the Edge!",
location: "Cloudflare Workers"
});
});
app.get("/api/:id", (req, res) => {
res.json({
id: req.params.id,
deployed: "globally"
});
});
// Export for Cloudflare Workers
export default {
fetch: app.fetch.bind(app),
};JavaScript
const { AzuraClient } = require("azurajs");
const app = new AzuraClient();
app.get("/", (req, res) => {
res.json({
message: "Hello from the Edge!",
location: "Cloudflare Workers"
});
});
app.get("/api/:id", (req, res) => {
res.json({
id: req.params.id,
deployed: "globally"
});
});
// Export for Cloudflare Workers
module.exports = {
fetch: app.fetch.bind(app),
};Why Use Cloudflare Workers?
- π Global deployment - 200+ edge locations
- β‘ 0ms cold starts - Instant responses
- π° Free tier - 100k requests/day
- π Built-in security - DDoS protection
Using with Node.js (Default) π’
AzuraJS includes a built-in Node.js HTTP server:
import { AzuraClient, applyDecorators } from "azurajs";
import { UserController } from "./controllers/UserController";
const app = new AzuraClient();
// Simple route
app.get("/", (req, res) => {
res.json({ message: "Hello from Node.js!" });
});
// Or use controllers
applyDecorators(app, [UserController]);
// Built-in Node.js HTTP server
await app.listen(3000);Run it:
node --import tsx node-server.ts
# or with Bun
bun run node-server.tsWhy Use Node.js?
- π¦ Largest ecosystem - npm packages
- π§ Mature tooling - Well-established
- π’ Production ready - Battle-tested
- π€ Wide support - Most hosting providers
Vercel Edge Functions πΊ
Deploy to Vercel's edge network:
import { AzuraClient } from "azurajs";
const app = new AzuraClient();
app.get("/api/hello", (req, res) => {
res.json({ message: "Hello from Vercel Edge!" });
});
export default async function handler(request: Request) {
return app.fetch(request);
}
export const config = {
runtime: "edge",
};Performance Comparison π
| Runtime | Cold Start | Throughput | Deploy |
|---|---|---|---|
| Bun | ~5ms | βββββ | Local/VPS |
| Node.js | ~50ms | ββββ | Anywhere |
| Deno | ~10ms | ββββ | Deno Deploy |
| Cloudflare | 0ms | βββββ | Global Edge |
| Vercel Edge | 0ms | ββββ | Vercel |
Complete Example π―
Here's a full example that works everywhere:
import { AzuraClient } from "azurajs";
import { Controller, Get, Post, Body, Param, Res } from "azurajs/decorators";
import type { ResponseServer } from "azurajs";
@Controller("/api")
class ApiController {
@Get("/")
root(@Res() res: ResponseServer) {
res.json({
message: "AzuraJS Universal API",
runtime: typeof Bun !== "undefined" ? "Bun" :
typeof Deno !== "undefined" ? "Deno" : "Node.js"
});
}
@Get("/users/:id")
getUser(@Param("id") id: string, @Res() res: ResponseServer) {
res.json({
id: Number(id),
name: `User ${id}`
});
}
@Post("/users")
createUser(@Body() body: any, @Res() res: ResponseServer) {
res.status(201).json({
id: Date.now(),
...body
});
}
}
const app = new AzuraClient();
applyDecorators(app, [ApiController]);
// Choose your runtime:
// Bun
if (typeof Bun !== "undefined") {
Bun.serve({ port: 3000, fetch: app.fetch.bind(app) });
}
// Deno
else if (typeof Deno !== "undefined") {
Deno.serve({ port: 3000 }, app.fetch.bind(app));
}
// Node.js (default)
else {
await app.listen(3000);
}Configuration Tips βοΈ
When using custom servers, you may want to disable some built-in features:
import type { ConfigTypes } from "azurajs/config";
const config: ConfigTypes = {
server: {
port: 3000,
cluster: false, // Disable for Bun/Deno/Edge
},
logging: {
enabled: true,
showDetails: false, // Less verbose for edge
},
};
export default config;Best Practices β¨
Use Bun for development - Fastest hot reload and startup time
Use Edge for production - Global deployment with 0ms cold starts
Test on Node.js - Ensure compatibility before deploying
Disable cluster mode - Not needed when using custom servers
