Skip to content

Bun

For: a developer who wants sign-in in a Bun server with a Postgres database.

What it does: you run the Node example app with Bun, through Bun.serve. A script signs a person in with a magic link through the real server.

An agent can do these steps with the authtempo MCP server. See Set up an agent.

  • Do the Node tutorial first. This tutorial uses the same app, examples/my-app.
  • Bun 1.4.3. The app pins it as a development tool, so pnpm install puts it in node_modules/.bin.

The command authtempo init my-app --stack bun writes the Node app with bun src/main.ts as its start script. authtempo init tells how.

  1. Read src/server-bun.ts. It serves the same handler with Bun.serve, and sets the client IP from Bun’s own call.
examples/my-app/src/server-bun.ts
import { CLIENT_IP_HEADER, type Serve } from "./app.ts";
export const serveBun: Serve = async (handler, options) => {
const server = Bun.serve({
port: options.port,
hostname: options.hostname,
fetch(request, srv) {
const headers = new Headers(request.headers);
headers.delete(CLIENT_IP_HEADER);
const ip = srv.requestIP(request)?.address;
if (ip) headers.set(CLIENT_IP_HEADER, ip);
return handler(new Request(request, { headers }));
},
});
return {
async close() {
await server.stop(true);
},
};
};
  1. Run the test of the app.
Terminal window
pnpm turbo run test --filter=my-app
  1. Find the line bun-e2e: PASS in the output. It comes after the line of the Node run.
  2. At the root of the repo, start the server with Bun and the settings of your deploy.
Terminal window
DATABASE_URL="postgres://USER:PASSWORD@HOST:5432/DATABASE" AUTH_SECRET="<32 characters or more, from openssl rand -base64 32>" BASE_URL="https://api.example.com" HOST=0.0.0.0 PORT=3000 pnpm --filter my-app exec bun src/main.ts

src/main.ts picks Bun.serve when it runs under Bun, and Node’s http module when it runs under Node. The app code is the same under both.