> ## Documentation Index
> Fetch the complete documentation index at: https://mintlify.com/oven-sh/bun/llms.txt
> Use this file to discover all available pages before exploring further.

# Express with Bun

> Run an Express.js server with Bun using Bun's built-in Node.js compatibility layer.

Express and other major Node.js HTTP libraries work with Bun out of the box. Bun implements the `node:http` and `node:https` modules that Express relies on.

<Note>
  See the [Node.js compatibility](/runtime/nodejs-compat) page for detailed information about which Node.js APIs Bun supports.
</Note>

## Setup

<Steps>
  <Step title="Install Express">
    ```bash theme={null}
    bun add express
    bun add -d @types/express
    ```
  </Step>

  <Step title="Create a server">
    ```typescript src/server.ts theme={null}
    import express from "express";

    const app = express();
    const port = parseInt(process.env.PORT ?? "3000");

    app.use(express.json());

    app.get("/", (req, res) => {
      res.send("Hello from Express on Bun!");
    });

    app.listen(port, () => {
      console.log(`Server running on http://localhost:${port}`);
    });
    ```
  </Step>

  <Step title="Run the server">
    ```bash theme={null}
    bun run src/server.ts
    ```
  </Step>
</Steps>

## Routing

Express routing works identically on Bun:

```typescript theme={null}
import express, { Request, Response } from "express";

const app = express();
app.use(express.json());

// GET with route parameter
app.get("/users/:id", (req: Request, res: Response) => {
  res.json({ id: req.params.id });
});

// POST with body
app.post("/users", (req: Request, res: Response) => {
  const { name, email } = req.body;
  res.status(201).json({ name, email });
});

// Router for grouping
const api = express.Router();
api.get("/status", (_req, res) => res.json({ ok: true }));

app.use("/api", api);

app.listen(3000);
```

## Middleware

Express middleware functions work the same way:

```typescript theme={null}
import express from "express";

const app = express();

// Built-in middleware
app.use(express.json());
app.use(express.urlencoded({ extended: true }));

// Custom logging middleware
app.use((req, _res, next) => {
  console.log(`${new Date().toISOString()} ${req.method} ${req.path}`);
  next();
});

// Error handling middleware
app.use((err: Error, _req: express.Request, res: express.Response, _next: express.NextFunction) => {
  console.error(err.stack);
  res.status(500).json({ error: err.message });
});

app.get("/", (_req, res) => res.send("OK"));
app.listen(3000);
```

## Performance notes

Express on Bun is faster than Express on Node.js because Bun's HTTP implementation is more efficient. However, if you are starting a new project and do not need Express compatibility, Bun's native `Bun.serve()` API or a Bun-native framework like [Elysia](/guides/elysia) or [Hono](/guides/hono) will provide better performance.

<Tabs>
  <Tab title="Express (Node.js compat)">
    ```typescript theme={null}
    import express from "express";

    const app = express();
    app.get("/", (_req, res) => res.send("Hello!"));
    app.listen(3000);
    ```
  </Tab>

  <Tab title="Bun.serve (native)">
    ```typescript theme={null}
    Bun.serve({
      port: 3000,
      fetch(req) {
        return new Response("Hello!");
      },
    });
    ```
  </Tab>
</Tabs>

<Tip>
  Most Express middleware from the npm ecosystem (body-parser, helmet, morgan, cors, etc.) works with Bun without any changes.
</Tip>
