Step 1: Install Bun
Bun is our all-in-one tool: it runs TypeScript, installs packages and restarts the server when files change.
# macOS or Linux$curl -fsSL https://bun.sh/install | bash# Windows (PowerShell)$powershell -c "irm bun.sh/install.ps1 | iex"# Close and reopen the terminal, then check it worked$bun --version
Step 2: Create the project
$mkdir tasks-api$cd tasks-api$bun init -y
bun init creates a few files for you. The important ones: package.json (your project's ID card and list of packages), tsconfig.json (TypeScript settings, already good) and .gitignore. You can delete the example index.ts; we'll put our code in a src folder.
Step 3: Install Express
$bun add express$bun add -d @types/express
The first line installs Express. The second installs its types (-d = only needed while developing), so TypeScript knows what req and res contain.
Step 4: Write your first server
Create a folder src and a file src/server.ts. This is a tiny To-Do API that keeps tasks in memory:
import express from "express";
const app = express();
app.use(express.json()); // read JSON bodies
type Task = { id: number; title: string; done: boolean };
// For now our "database" is just an array in memory. It resets when the server restarts.
const tasks: Task[] = [];
app.get("/health", (req, res) => {
res.json({ status: "ok" });
});
app.get("/api/tasks", (req, res) => {
res.json(tasks);
});
app.post("/api/tasks", (req, res) => {
const title = String(req.body.title ?? "").trim();
if (!title) {
return res.status(400).json({ error: "Title is required" });
}
const task: Task = { id: tasks.length + 1, title, done: false };
tasks.push(task);
res.status(201).json(task);
});
app.listen(3000, () => {
console.log("API running on http://localhost:3000");
});Read it top to bottom, it's only five ideas:
express()creates the app.app.use(express.json())lets Express read JSON that the app sends in the body.app.get(path, handler)andapp.post(path, handler)say “when this request arrives, run this function”. These are routes.- Inside a handler,
reqis the request (what came in) andresis the response (what you send back). app.listen(3000)starts the server on port 3000.
Step 5: Add scripts and run it
Open package.json and add a scripts section, so you don't have to remember long commands:
{
"name": "tasks-api",
"type": "module",
"private": true,
"scripts": {
"dev": "bun --watch src/server.ts",
"start": "bun src/server.ts",
"typecheck": "bun check"
}
}$bun run dev
You'll see API running on http://localhost:3000. Thanks to --watch, every time you save a file the server restarts by itself.
Step 6: Try your API
Open http://localhost:3000/health in your browser, or use curl in a second terminal:
$curl http://localhost:3000/health# Add a task$curl -X POST http://localhost:3000/api/tasks -H "Content-Type: application/json" -d '{"title":"Buy milk"}'# Try an empty title$curl -X POST http://localhost:3000/api/tasks -H "Content-Type: application/json" -d '{"title":""}'# List all tasks$curl http://localhost:3000/api/tasks
These are the real answers our server gave when we tested it:
{
"status": "ok"
}{
"id": 1,
"title": "Buy milk",
"done": false
}{
"error": "Title is required"
}[
{
"id": 1,
"title": "Buy milk",
"done": false
}
]Settings go in a .env file
Things that change between your laptop and the real server (port, database address, secret keys) never go in the code. They go in a file called .env in the project folder:
PORT=3000
DATABASE_URL="postgresql://USER:PASSWORD@localhost:5432/tasks_app"
JWT_SECRET="change-me-to-a-long-random-string"Bun loads .env automatically, so you can read process.env.PORT in your code. The file is already in .gitignore, which means it's never uploaded to GitHub. Commit a copy called .env.example with fake values, so others know which settings exist.
Bun and npm commands side by side
If you ever work on a project that uses npm and Node.js, here's the translation:
| Bun | npm / Node.js | What it does |
|---|---|---|
| bun init | npm init -y | Start a new project |
| bun install | npm install | Install everything in package.json |
| bun add express | npm install express | Add a package |
| bun add -d @types/express | npm install -D @types/express | Add a dev-only package |
| bun remove express | npm uninstall express | Remove a package |
| bun run dev | npm run dev | Run a script from package.json |
| bun src/server.ts | npx tsx src/server.ts | Run a TypeScript file |
| bunx prisma | npx prisma | Run a package's command without installing it globally |
Key takeaways
- ✓Bun installs packages, runs TypeScript directly and restarts on save, all in one tool.
- ✓An Express app is: create the app, add middleware like express.json(), add routes, listen on a port.
- ✓Each route gets req (the request) and res (the response).
- ✓Our To-Do API now has GET /api/tasks and POST /api/tasks, but data lives in memory for now.
- ✓Settings like the port and database address go in .env.
Frequently asked questions
What is Bun?
Bun is one fast tool that does several jobs: it runs JavaScript and TypeScript, installs packages (like npm), runs scripts, and watches files for changes. It runs Node.js-style code, so Express works on it.
Can I use npm and Node.js instead of Bun?
Yes. Everything in this course works with Node.js too. The table in this lesson shows the npm version of each command. With Node.js you also need a tool like tsx to run TypeScript during development.
What is Express?
Express is a small library that makes writing APIs easy: you say 'when someone sends GET /api/tasks, run this function'. It's the most used web framework for Node.js.
Why does my data disappear when I restart the server?
Because in this lesson tasks live in an array in memory. Memory is wiped when the program stops. Two lessons from now we save them in PostgreSQL, which keeps data forever.
What is localhost:3000?
localhost means 'this computer'. 3000 is the port, like a door number. Your API listens at that door while it runs.
🧠 Quick check: your first server
1. Which command installs Express into your project with Bun?
2. What does app.use(express.json()) do?
3. You created a task. Which line sends the right response?
4. Why use bun --watch during development?
Checking your access…