5-minute quickstart

Install Bun, create a TypeScript HTTP service, and run a test

Last updated on

What you will build

A TypeScript HTTP service with no extra framework, one real test, and a minimal project structure.

1. Install and verify

curl -fsSL https://bun.com/install | bash
bun --version
bun --revision

Linux needs unzip. If the command is missing, reopen your terminal and verify that ~/.bun/bin is on PATH.

2. Create the project

mkdir hello-bun
cd hello-bun
bun init -y
server.ts
const server = Bun.serve({
  port: 3000,
  routes: {
    '/': new Response('Hello from Bun!'),
    '/health': Response.json({ ok: true }),
  },
});

console.log(`Listening on ${server.url}`);
bun run server.ts
curl http://localhost:3000/health
# {"ok":true}

3. Add a test

math.ts
export function add(a: number, b: number) {
  return a + b;
}
math.test.ts
import { expect, test } from 'bun:test';
import { add } from './math';

test('adds two numbers', () => {
  expect(add(2, 3)).toBe(5);
});
bun test

4. Save repeatable scripts

package.json
{
  "scripts": {
    "dev": "bun --watch server.ts",
    "start": "bun run server.ts",
    "test": "bun test"
  }
}

A runtime is not an operations plan

Before production, decide how the process is supervised, logged, health-checked, configured, and gracefully terminated on your target platform.

Next, read the mental model or the Node.js migration guide.

Sources: Installation, Bun.serve, and Test runner.