AzuraJS Logo
AzuraJSFramework
v2.2 Beta

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

bun-server.ts
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

bun-server.js
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 .js

Why 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

deno-server.ts
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

deno-server.js
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 .js

Why 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

worker.ts
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

worker.js
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:

node-server.ts
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.ts

Why 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:

api/hello.ts
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 πŸ“Š

RuntimeCold StartThroughputDeploy
Bun~5ms⭐⭐⭐⭐⭐Local/VPS
Node.js~50ms⭐⭐⭐⭐Anywhere
Deno~10ms⭐⭐⭐⭐Deno Deploy
Cloudflare0ms⭐⭐⭐⭐⭐Global Edge
Vercel Edge0ms⭐⭐⭐⭐Vercel

Complete Example 🎯

Here's a full example that works everywhere:

universal-app.ts
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:

azura.config.ts
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

Next Steps πŸ“–

On this page