Next-Gen Microservice Gateways: Real-Time Forwarding with Bun

Seyyed Ali Mohammadiyeh (Max Base)
Node.js Global Summit 25 - May 20, 2025

Node.js Global Summit 2025

About Me

Seyyed Ali Mohammadiyeh (Max Base)
Open-source Maintainer, GitHub
Senior Software Engineer
CTO, asrez
📧 maxbasecode@gmail.com

Node.js Global Summit 2025

Experience

  • 👨‍💻 GitHub: https://github.com/basemax
  • 🧠 10+ years of experience in software development
  • 🎓 Background in pure and applied mathematics with research experience
Node.js Global Summit 2025

Session Overview

Explore how Bun and TypeScript enable the creation of a high-performance, real-time gateway that routes incoming HTTP requests to the appropriate microservices.

  • Why traditional reverse proxies (like NGINX or Express) fall short in dynamic routing scenarios.
  • How Bun offers minimal latency, simplified logic, and better scalability.
Node.js Global Summit 2025

This talk includes:

✅ Real-world architecture & benchmarks
✅ Code examples
✅ Lessons from production

Node.js Global Summit 2025

Goals of This Talk

  1. Learn to build a real-time microservice gateway using Bun + TypeScript
  2. Discover how Bun can outperform Express/NGINX in dynamic routing
  3. Understand live request forwarding with minimal latency
Node.js Global Summit 2025

Getting Started with Bun

$ bun init

✓ Select a project template: Blank

 + .gitignore
 + index.ts
 + tsconfig.json (for editor autocomplete)

To get started, run:
    bun run index.ts
// index.ts
console.log("Hello via Bun!");
Node.js Global Summit 2025

Running your first Bun program

$ bun run index.ts
Hello via Bun!
Node.js Global Summit 2025

Why Bun?

✅ Native HTTP server (no need for Express or external frameworks)
✅ Written in TypeScript
✅ Built-in routing based on URL, headers, or tokens
✅ Blazing fast
✅ Zero dependencies

Downsides:
⚠️ Type checking and runtime validation in TypeScript is still maturing.
⚠️ Limited ecosystem maturity compared to Node.js

Node.js Global Summit 2025

Using Bun.serve()

const server = Bun.serve({
  port: 9999,
  fetch(req) {
    return new Response("Not Found", { status: 404 });
  },
});
  • Every incoming request triggers the fetch() function.
  • You can access request details through the req object.
Node.js Global Summit 2025

Accessing Request Details

const server = Bun.serve({
  port: 9999,
  fetch(req) {
    const url = new URL(req.url);
    console.log(url);
    return new Response("Not Found", { status: 404 });
  },
});
Node.js Global Summit 2025

Example req (request) object:

Request {
  method: "GET",
  url: "http://localhost:9999/",
  headers: Headers {
    "host": "localhost:9999",
    "user-agent": "...",
    ...
  }
}
Node.js Global Summit 2025

Key values to use:

  • req.method
  • req.url
  • req.headers
Node.js Global Summit 2025

Parsed URL Object

Output of new URL(req.url):

URL {
  href: "http://localhost:9999/favicon.ico",
  origin: "http://localhost:9999",
  pathname: "/favicon.ico",
  searchParams: URLSearchParams {}
}
Node.js Global Summit 2025

Let's Create a Microservice Gateway

Imagine we are working on a large-scale project with over 800 RESTful API routes.

From an architectural perspective, we can split the entire platform into 20 sub-projects.

Each of these 20 sub-projects runs independently and serves on a different HTTP port.

Node.js Global Summit 2025

Let's Create a Microservice Gateway

Now, we need a central gateway — a program that initially receives all incoming requests from end users. It should analyze each request to determine which sub-project or microservice it belongs to and then forward the request accordingly.

The gateway must not only forward the request to the appropriate sub-project but also handle the response by forwarding it back to the user.

Node.js Global Summit 2025

Let's create a config structure

{
  "host": "0.0.0.0",
  "port": 9999,
  "services": [
    {
      "prefix": "/user/",
      "host": "localhost",
      "port": 8888
    },
    {
      "path": "/my/login/",
      "method": "POST",
      "host": "localhost",
      "port": 6666
    },
    ...
  ]
}
Node.js Global Summit 2025

Coding

Create a new Bun project:

bun init
Node.js Global Summit 2025

Wrap Up

  • Bun + TypeScript = high-speed real-time gateways
  • Ideal for scalable microservices with dynamic routing
  • Simpler, faster, and more maintainable than traditional tools

Thank you!

🔗 github.com/basemax
📧 maxbasecode@gmail.com

Node.js Global Summit 2025