Ultra-performante Web-APIs mit Hono: Cloudflare Workers, Bun und ElysiaJS

Wie Hono als flexibles Framework auf verschiedenen Plattformen schnelle, skalierbare Web-Backends ermöglicht.

10 Minuten
Ultra-performante Web-APIs mit Hono: Cloudflare Workers, Bun und ElysiaJS
#Hono #Cloudflare Workers #Bun #ElysiaJS

1. Einleitung

Die Entwicklung moderner Webanwendungen erfordert schnelle, skalierbare und wartungsarme Backends. Dieser Artikel zeigt, wie Hono als minimalistisches Web-Framework auf verschiedenen Plattformen eingesetzt werden kann:

  • Hono auf Cloudflare Workers für Edge-Computing und globale Verfügbarkeit
  • Hono mit Bun für lokale Entwicklung und traditionelles Hosting
  • ElysiaJS mit Bun als Alternative mit noch tieferer Bun-Integration

Wir klären die Unterschiede, Vor- und Nachteile der verschiedenen Ansätze.

2. Überblick der Technologien

2.1 Hono

Hono ist ein minimalistisches Web-Framework mit nur 14 KB Größe. Seine API orientiert sich an Express, ist aber für moderne Web-Standards wie die Request- und Response-API optimiert. Der große Vorteil: Hono läuft überall – auf Cloudflare Workers, Deno, Bun, Node.js und mehr.

Wichtige Klarstellung: Hono ist platform-agnostic. Du kannst denselben Hono-Code auf verschiedenen Runtimes laufen lassen.

2.2 Cloudflare Workers

Cloudflare Workers ist eine Serverless-Plattform für Edge-Computing. Code läuft direkt in Cloudflares globalem Netzwerk:

  • Global verteilte Ausführung: Anfragen werden vom nächstgelegenen Rechenzentrum bearbeitet
  • Geringe Latenz: Durchschnittlich < 50ms Response-Zeit weltweit
  • Built-in Services: KV Storage, D1 (SQL), R2 (Object Storage), Durable Objects
  • Web-Standard-APIs: Verwendet Request/Response statt Node.js-APIs

Wichtig: Cloudflare Workers nutzt die V8-Engine mit Web-Standard-APIs. Bun läuft NICHT auf Cloudflare Workers. Hono hingegen funktioniert auf Workers, weil es keine Runtime-spezifischen Features nutzt.

2.3 Bun

Bun ist eine moderne JavaScript-Runtime mit integriertem Transpiler, Test-Runner und Package-Manager. Vorteile:

  • Sehr schnell: Deutlich schneller als Node.js
  • All-in-One: Bundler, Transpiler, Package Manager eingebaut
  • Node.js-kompatibel: Kann Node.js-Packages nutzen
  • Native TypeScript: Kein separater Build-Step nötig

Einsatzgebiet: Lokale Entwicklung oder traditionelles Server-Hosting. Nicht für Cloudflare Workers geeignet.

3. Option 1: Hono auf Cloudflare Workers

Hono ist ideal für Cloudflare Workers, weil es nur Web-Standard-APIs nutzt.

3.1 Projekt erstellen

npm create hono@latest my-worker
cd my-worker
npm install

Wähle bei der Einrichtung “cloudflare-workers” als Template.

3.2 Beispiel-Code (src/index.ts)

import { Hono } from 'hono'

const app = new Hono()

app.get('/', (c) => c.text('Running on Cloudflare Workers!'))

export default app

Wichtig: Kein app.listen() oder Server-Setup nötig. Cloudflare Workers exportiert einfach die App.

3.3 Deployment-Konfiguration (wrangler.toml)

name = "hono-worker"
main = "src/index.ts"
compatibility_date = "2025-11-01"

3.4 Deployment

npx wrangler deploy

Die App läuft nun global verteilt auf Cloudflare’s Edge-Netzwerk.

4. Option 2: Hono mit Bun (lokales Hosting)

Wenn du Bun nutzen möchtest, läuft Hono lokal oder auf einem traditionellen Server, nicht auf Cloudflare Workers.

4.1 Projekt erstellen

bun create hono my-api
cd my-api
bun install

4.2 Beispiel-Code mit Bun-Server (src/index.ts)

import { Hono } from 'hono'

const app = new Hono()

app.get('/', (c) => c.text('Hello from Bun + Hono!'))

export default {
  port: 3000,
  fetch: app.fetch,
}

4.3 Entwicklung & Produktion

# Entwicklung
bun run --hot src/index.ts

# Produktion
bun run src/index.ts

4.4 Deployment-Optionen für Bun

Da Bun nicht auf Cloudflare Workers läuft, brauchst du traditionelles Hosting:

Docker (empfohlen):

FROM oven/bun:1
WORKDIR /app
COPY package.json bun.lockb ./
RUN bun install --production
COPY . .
EXPOSE 3000
CMD ["bun", "run", "src/index.ts"]

Hosting-Optionen:

  • VPS (Virtual Private Server) mit Docker
  • Container-Plattformen (die Bun unterstützen)
  • Eigene Server-Infrastruktur

Nicht geeignet: Cloudflare Workers, Vercel Edge Functions, Netlify Edge Functions (alle nutzen V8/Deno, nicht Bun)

5. Option 3: ElysiaJS mit Bun (Bun-native)

Wenn du ausschließlich auf Bun setzen willst, ist ElysiaJS eine interessante Alternative zu Hono. ElysiaJS ist speziell für Bun entwickelt und nutzt dessen Features optimal.

5.1 Was ist ElysiaJS?

ElysiaJS ist ein Web-Framework, das exklusiv für Bun entwickelt wurde. Vorteile:

  • Bun-native: Nutzt Bun-spezifische Optimierungen
  • Type-Safety: Ende-zu-Ende TypeScript-Typen (auch für Requests/Responses)
  • Automatische OpenAPI-Docs: Generiert API-Dokumentation automatisch
  • Validierung eingebaut: Zod-basierte Schema-Validierung out-of-the-box
  • Sehr performant: Optimiert für Bun’s Geschwindigkeit

5.2 Beispiel: ElysiaJS Hello World

bun create elysia my-elysia-app
cd my-elysia-app
import { Elysia } from 'elysia'

const app = new Elysia()
  .get('/', () => 'Hello from Elysia!')
  .get('/json', () => ({ message: 'API response' }))
  .listen(3000)

console.log(`Server running at http://localhost:${app.server?.port}`)

5.3 Type-Safe APIs mit ElysiaJS

import { Elysia, t } from 'elysia'

const app = new Elysia()
  .post('/user', ({ body }) => {
    // body ist automatisch typisiert!
    return {
      id: 1,
      name: body.name,
      email: body.email
    }
  }, {
    body: t.Object({
      name: t.String(),
      email: t.String({ format: 'email' })
    })
  })
  .listen(3000)

ElysiaJS validiert automatisch und generiert TypeScript-Typen für Request/Response.

5.4 Wann ElysiaJS statt Hono?

ElysiaJS wählen, wenn:

  • Du nur Bun nutzen willst (nicht Multi-Platform)
  • Du End-to-End Type-Safety brauchst
  • Du automatische API-Docs willst
  • Du maximale Performance auf Bun suchst

Hono wählen, wenn:

  • Du Platform-Flexibilität brauchst (Cloudflare Workers, Deno, Node.js)
  • Du zwischen verschiedenen Runtimes wechseln möchtest
  • Du Express-ähnliche API bevorzugst

5.5 Deployment für ElysiaJS

Da ElysiaJS Bun benötigt, gelten dieselben Deployment-Optionen wie bei Hono + Bun:

Docker:

FROM oven/bun:1
WORKDIR /app
COPY package.json bun.lockb ./
RUN bun install --production
COPY . .
EXPOSE 3000
CMD ["bun", "run", "src/index.ts"]

Hosting:

  • VPS mit Docker
  • Container-Plattformen mit Bun-Support
  • Eigene Server

Nicht kompatibel: Cloudflare Workers, Vercel Edge, Netlify Edge (benötigen V8/Deno-Runtime)

6. Hono Features: Rate Limiting, Caching & SSR mit JSX

Die Stärke von Hono liegt in seiner erweiterbaren Middleware-Architektur.

5.1 Rate Limiting mit Middleware

Ein Rate Limiting zum Schutz der API lässt sich mit einer vorgefertigten Middleware einfach hinzufügen:

import { Hono } from 'hono'
import { rateLimit } from 'hono-rate-limit'

const app = new Hono()

app.use('*', rateLimit({ windowMs: 60_000, limit: 100 }))

// ... rest of the routes

5.2 Caching mit Cloudflare

Das Caching auf Cloudflare Workers kann direkt über die Standard-Cache-API gesteuert werden:

app.get('/cached', async (c) => {
  const cache = caches.default
  const url = new URL(c.req.url)
  const key = new Request(url.toString(), c.req)
  const cachedResponse = await cache.match(key)

  if (cachedResponse) {
    console.log('Cache hit!')
    return cachedResponse
  }

  const response = new Response('Fresh content from the edge!', {
    headers: { 'Cache-Control': 's-maxage=60' },
  })
  c.executionCtx.waitUntil(cache.put(key, response.clone()))

  return response
})

5.3 Server Side Rendering mit JSX

Dank des JSX-Supports kann Hono auch für serverseitig gerenderte HTML-Seiten verwendet werden:

import { jsx } from 'hono/jsx'

const Layout = (props: { children?: any }) => {
  return (
    <html>
      <body>{props.children}</body>
    </html>
  )
}

app.get('/ssr', (c) => {
  return c.html(<Layout><h1>Hello from SSR JSX!</h1></Layout>)
})

Hinweis: Um JSX zu nutzen, muss die tsconfig.json entsprechend konfiguriert werden.

7. Fazit: Welche Kombination wählen?

Für Edge-Computing und globale Verfügbarkeit:

  • Hono + Cloudflare Workers ist ideal
  • Extrem niedrige Latenz weltweit
  • Keine Server-Verwaltung nötig
  • Skaliert automatisch

Für traditionelles Hosting mit Bun:

  • Hono + Bun wenn du Platform-Flexibilität brauchst
  • ElysiaJS + Bun wenn du maximale Bun-Integration und Type-Safety willst
  • Deployment via Docker oder VPS
  • Volle Kontrolle über Infrastruktur

Wichtige Klarstellung: Bun läuft nicht auf Cloudflare Workers. Hono ist platform-agnostic und funktioniert auf beiden – aber du musst dich für eine Runtime-Umgebung entscheiden:

  • Workers = V8-Engine mit Web-Standards
  • Bun = Eigene Runtime mit Node.js-Kompatibilität

Die Frameworks (Hono, ElysiaJS) sind exzellent – die Wahl hängt von deiner Deployment-Strategie ab.