Bun for Full Stack
×


Bun for Full Stack

107

๐Ÿงฉ Bun for Full Stack Development

Full stack development has traditionally required juggling multiple tools โ€” Node.js for backend, Webpack or Vite for bundling, Jest for testing, and npm for package management. But now there's Bun โ€” an all-in-one toolkit that can power your full stack app with blazing speed โšก.

๐Ÿš€ Why Choose Bun for Full Stack?

Bun isn't just a JavaScript runtime โ€” it's also a bundler, transpiler, test runner, and package manager. This means:

  • ๐Ÿ“ฆ Faster installs with bun install
  • ๐Ÿงช Built-in testing with bun test
  • ๐Ÿš€ Native bundling and transpilation
  • ๐Ÿง  Fewer dependencies to configure

By reducing complexity and speeding up development, Bun helps full stack developers focus more on building โ€” not tool-chaining.

๐Ÿ”ง Setting Up a Full Stack Project with Bun

Letโ€™s scaffold a basic full stack app with Bun. Backend will use Bunโ€™s native server and the frontend will be a vanilla HTML+JS app served by the same server.

// index.ts - Backend entry point
Bun.serve({
  port: 3000,
  fetch(req) {
    const url = new URL(req.url);

    if (url.pathname === "/api/hello") {
      return Response.json({ message: "๐Ÿ‘‹ Hello from Bun API" });
    }

    return new Response(Bun.file("public/index.html"));
  }
});

Create a public/index.html file:

<!DOCTYPE html>
<html>
  <head><title>Bun Full Stack</title></head>
  <body>
    <h1>๐Ÿง‘โ€๐Ÿ’ป Full Stack with Bun</h1>
    <button onclick="loadData()">Fetch API</button>
    <pre id="output"></pre>
    <script>
      async function loadData() {
        const res = await fetch('/api/hello');
        const data = await res.json();
        document.getElementById('output').textContent = JSON.stringify(data, null, 2);
      }
    </script>
  </body>
</html>

Thatโ€™s it โ€” youโ€™ve got a working full stack app with zero build config and one command: bun index.ts โœ…

๐Ÿ› ๏ธ Add a Simple Database (SQLite)

Letโ€™s expand the backend with a simple database using Bunโ€™s SQLite driver:

const db = new Database("data.db");
db.run("CREATE TABLE IF NOT EXISTS messages (id INTEGER PRIMARY KEY, text TEXT)");

Bun.serve({
  port: 3000,
  async fetch(req) {
    const url = new URL(req.url);

    if (url.pathname === "/api/message" && req.method === "POST") {
      const body = await req.json();
      db.run("INSERT INTO messages (text) VALUES (?)", [body.text]);
      return Response.json({ status: "Saved โœ…" });
    }

    if (url.pathname === "/api/message") {
      const messages = db.query("SELECT * FROM messages").all();
      return Response.json(messages);
    }

    return new Response(Bun.file("public/index.html"));
  }
});

Now youโ€™ve got a working database layer for your full stack Bun app ๐Ÿ“ฅ.

๐Ÿงช Testing Both Frontend & Backend

Bun has a native test runner that supports both backend and frontend logic. Here's an example of an API unit test:

import { test, expect } from "bun:test";

test("basic API response", async () => {
  const res = await fetch("http://localhost:3000/api/hello");
  const data = await res.json();
  expect(data.message).toBe("๐Ÿ‘‹ Hello from Bun API");
});

No extra libraries or configs needed. Just run bun test โœ….

๐Ÿ“ฆ Handling Frontend Builds

You can bundle and transpile frontend assets using Bun too:

bun build public/main.js --outdir=dist --minify

It supports JSX, TypeScript, and ES6 out of the box. So even React or Preact-based frontends are easy to set up with Bun ๐ŸŒŸ.

๐Ÿ’ก Full Stack Project Ideas with Bun

  • ๐Ÿ—‚๏ธ Task manager (SQLite + native APIs)
  • ๐Ÿ“ฌ Real-time chat app (WebSocket support)
  • ๐Ÿ“Š Analytics dashboard (Bun + React + D3)
  • ๐Ÿ“š Blog engine (Markdown rendering, SQLite)

๐Ÿ“ Folder Structure Example

project/
โ”œโ”€โ”€ index.ts
โ”œโ”€โ”€ db/
โ”‚   โ””โ”€โ”€ sqlite.ts
โ”œโ”€โ”€ public/
โ”‚   โ”œโ”€โ”€ index.html
โ”‚   โ””โ”€โ”€ main.js
โ”œโ”€โ”€ tests/
โ”‚   โ””โ”€โ”€ api.test.ts

๐ŸŒ Going Further: Add SSR or APIs

You can integrate Bun with server-side rendering (SSR) frameworks like Elysia or use it to build REST APIs, GraphQL APIs, or static site generators.

Itโ€™s flexible enough to serve as both the frontend engine and the backend server ๐Ÿ› ๏ธ.

๐Ÿ Final Thoughts

Bun for full stack development is a game changer. It combines speed, simplicity, and flexibility in one powerful toolset. Whether youโ€™re building a small prototype or a production-ready app, Bun can handle the frontend, backend, and everything in between.



If youโ€™re passionate about building a successful blogging website, check out this helpful guide at Coding Tag โ€“ How to Start a Successful Blog. It offers practical steps and expert tips to kickstart your blogging journey!

For dedicated UPSC exam preparation, we highly recommend visiting www.iasmania.com. It offers well-structured resources, current affairs, and subject-wise notes tailored specifically for aspirants. Start your journey today!


Best WordPress Hosting


Share:


Discount Coupons

Get a .COM for just $6.98

Secure Domain for a Mini Price



Leave a Reply


Comments
    Waiting for your comments

Coding Tag WhatsApp Chat