Skip to main content

Overview

Build a minimal HTTP server with Bun.serve, run it locally, then evolve it by installing a package.
Prerequisites: Bun installed and available on your PATH. See installation for setup.

1

Step 1

Initialize a new project with bun init.
terminal
bun init prompts you to pick a template: Blank, React, or Library. For this guide, pick Blank.
terminal
The new my-app directory contains a basic Bun app.
2

Step 2

Run index.ts with bun run.
terminal
3

Step 3

Replace the contents of index.ts with the following code:
index.ts
Run index.ts again.
terminal
Visit http://localhost:3000 to test the server. You should see a page that says "Bun!".
bun init installs Bun’s TypeScript declarations and configures your tsconfig.json. If you’re trying out Bun in an existing project, you may see a type error on the Bun global.To fix this, first install @types/bun as a dev dependency.
terminal
Then add the following to your compilerOptions in tsconfig.json:
tsconfig.json
4

Step 4

Install the figlet package and its type declarations. Figlet is a utility for converting strings into ASCII art.
terminal
Update index.ts to use figlet in routes.
index.ts
Run index.ts again.
terminal
Visit http://localhost:3000/figlet to test the server. You should see a page that says "Bun!" in ASCII art.
5

Step 5

Now add some HTML. Create a new file called index.html:
index.html
Then, import this file in index.ts and serve it from the root / route.
index.ts
Run index.ts again.
terminal
Visit http://localhost:3000 to test the server. You should see the static HTML page.
You’ve built an HTTP server with Bun and installed a package.

Run a script

Bun can also execute "scripts" from your package.json. Add the following script:
package.json
Then run it with bun run start.
terminal
⚡️ Performancebun run is roughly 28x faster than npm run (6ms vs 170ms of overhead).