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.
Setup
Install Express
bun add express
bun add -d @types/express
Create a server
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}`);
});
Routing
Express routing works identically on Bun:
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:
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);
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 or Hono will provide better performance.
Express (Node.js compat)
Bun.serve (native)
import express from "express";
const app = express();
app.get("/", (_req, res) => res.send("Hello!"));
app.listen(3000);
Bun.serve({
port: 3000,
fetch(req) {
return new Response("Hello!");
},
});
Most Express middleware from the npm ecosystem (body-parser, helmet, morgan, cors, etc.) works with Bun without any changes.