Skip to content

Bun.serve et le futur HTTP TypeScript #271

Description

@khalilbenaz

lede: Bun.serve : serveur HTTP intégré 2-3× plus rapide qu'Express. API standard Web. Compatible Hono et frameworks modernes.
lede_en: Bun.serve: integrated HTTP server 2-3× faster than Express. Standard Web API. Compatible with Hono and modern frameworks.
title_en: Bun.serve and the future of TypeScript HTTP

Bun.serve — l'intégré

Bun runtime intègre Bun.serve() : serveur HTTP natif, performant.

Bun.serve({
  port: 3000,
  fetch(req) {
    const url = new URL(req.url);
    if (url.pathname === "/") return new Response("Hello");
    if (url.pathname === "/api/users") return Response.json([{ id: 1 }]);
    return new Response("Not Found", { status: 404 });
  },
});

Pas d'imports externes. API standard Web (Request/Response).

Performance

Sur benchmarks simples :

  • Bun.serve : ~150k req/s.
  • Hono on Bun : ~140k req/s.
  • Fastify on Node : ~50k req/s.
  • Express on Node : ~20k req/s.

3-7× plus rapide que Node typique.

Features

  • HTTPS/2 natif.
  • WebSocket intégré.
  • Streaming Request/Response.
  • TLS built-in.
Bun.serve({
  port: 3000,
  tls: {
    cert: Bun.file("cert.pem"),
    key: Bun.file("key.pem"),
  },
  fetch(req) {
    return new Response("Hello HTTPS");
  },
});

WebSocket

Bun.serve({
  fetch(req, server) {
    if (server.upgrade(req)) return; // WebSocket
    return new Response("Hello");
  },
  websocket: {
    open(ws) { ws.send("Connected"); },
    message(ws, msg) { ws.send(`Echo: ${msg}`); },
  },
});

Une API pour HTTP + WebSocket.

Compatible avec frameworks

import { Hono } from 'hono';
const app = new Hono();
app.get('/', (c) => c.text('Hello'));

Bun.serve({ fetch: app.fetch });

Hono, Elysia, et tous les frameworks compatibles Web API marchent sur Bun.serve.

Vs Cloudflare Workers

Bun.serve = même API que Workers (Request/Response standard). Vous pouvez écrire du code et déployer sur :

  • Bun (self-host).
  • Cloudflare Workers.
  • Vercel Edge.
  • Deno Deploy.

Multi-runtime sans changement.

Limites

  • Pas dans Node : Bun-only.
  • Maturité production : moindre que Express/Fastify.
  • Recruitment Bun devs : encore restreint.

Verdict

Pour nouveaux projets TS modernes en 2026 : Bun.serve sweet spot performance. Pour enterprise stable : Fastify/Node reste plus sûr.

Bun progresse rapidement. À surveiller.

Bun.serve — the integrated

Bun runtime integrates Bun.serve(): native, performant HTTP server.

Bun.serve({
  port: 3000,
  fetch(req) {
    const url = new URL(req.url);
    if (url.pathname === "/") return new Response("Hello");
    if (url.pathname === "/api/users") return Response.json([{ id: 1 }]);
    return new Response("Not Found", { status: 404 });
  },
});

No external imports. Standard Web API (Request/Response).

Performance

On simple benchmarks:

  • Bun.serve: ~150k req/s.
  • Hono on Bun: ~140k req/s.
  • Fastify on Node: ~50k req/s.
  • Express on Node: ~20k req/s.

3-7× faster than typical Node.

Features

  • Native HTTPS/2.
  • Integrated WebSocket.
  • Request/Response streaming.
  • Built-in TLS.
Bun.serve({
  port: 3000,
  tls: {
    cert: Bun.file("cert.pem"),
    key: Bun.file("key.pem"),
  },
  fetch(req) {
    return new Response("Hello HTTPS");
  },
});

WebSocket

Bun.serve({
  fetch(req, server) {
    if (server.upgrade(req)) return; // WebSocket
    return new Response("Hello");
  },
  websocket: {
    open(ws) { ws.send("Connected"); },
    message(ws, msg) { ws.send(`Echo: ${msg}`); },
  },
});

One API for HTTP + WebSocket.

Compatible with frameworks

import { Hono } from 'hono';
const app = new Hono();
app.get('/', (c) => c.text('Hello'));

Bun.serve({ fetch: app.fetch });

Hono, Elysia, all Web API-compatible frameworks work on Bun.serve.

vs Cloudflare Workers

Bun.serve = same API as Workers (standard Request/Response). You can write code and deploy to:

  • Bun (self-host).
  • Cloudflare Workers.
  • Vercel Edge.
  • Deno Deploy.

Multi-runtime without changes.

Limits

  • Not in Node: Bun-only.
  • Production maturity: less than Express/Fastify.
  • Bun dev recruitment: still restricted.

Verdict

For new modern TS projects in 2026: Bun.serve performance sweet spot. For stable enterprise: Fastify/Node stays safer.

Bun progresses fast. Worth watching.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions