๐ŸŽ“
MASTERCLASS๋‚œ์ด๋„: ์ดˆ๊ธ‰ โž” ์‹ค์ „โฑ๏ธ ์•ฝ 2.5์‹œ๊ฐ„๐Ÿš€ 4๋‹จ๊ณ„ ์ปค๋ฆฌํ˜๋Ÿผ

Bun & ElysiaJS ์ดˆ๊ณ ์† ํƒ€์ž… ์•ˆ์ „ API ์„œ๋ฒ„ ์‹ค์ „

Bun v1.4.5 ๋„ค์ดํ‹ฐ๋ธŒ ๋Ÿฐํƒ€์ž„๊ณผ ElysiaJS v1.4.32๋กœ TypeBox ์Šคํ‚ค๋งˆ ๊ฒ€์ฆ, Swagger/OpenAPI ์ž๋™ ๋ฌธ์„œํ™”, Eden Treaty ์ œ๋กœ ์ฝ”๋“œ์   End-to-End RPC ๋ฐ WebSocket Pub/Sub๊นŒ์ง€ ๊ตฌ์ถ•ํ•˜๋Š” ์‹ค์ „ ๊ฐ•์ขŒ

--views
#bun#elysiajs#typescript#eden-treaty#openapi#websocket#sqlite#tutorial์ตœ์ข… ์—…๋ฐ์ดํŠธ: 2026-11-29

0. ๐ŸŒŸ ์™œ โ€˜Bun & ElysiaJSโ€™ ์กฐํ•ฉ์ธ๊ฐ€์š”?

Node.js ๊ธฐ๋ฐ˜ ์„œ๋ฒ„์—์„œ๋Š” ๋นŒ๋“œ ๋„๊ตฌ(tsc/tsx), ํ…Œ์ŠคํŠธ ๋Ÿฌ๋„ˆ(vitest), SQLite ๋“œ๋ผ์ด๋ฒ„, OpenAPI ๋ฌธ์„œ ์ƒ์„ฑ๊ธฐ๋ฅผ ๊ฐ๊ฐ ์กฐ๋ฆฝํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. Bun v1.4.5๋Š” ์ด ๋ชจ๋“  ๊ฒƒ์„ ๋‹จ์ผ ์ดˆ๊ณ ์† ๋ฐ”์ด๋„ˆ๋ฆฌ์— ๋‚ด์žฅํ–ˆ๊ณ , ElysiaJS v1.4.32๋Š” Bun์˜ ๋„ค์ดํ‹ฐ๋ธŒ Bun.serve ์œ„์—์„œ ๋™์ž‘ํ•˜๋ฉฐ ์ฝ”๋“œ ์ƒ์„ฑ ์—†์ด ํ”„๋ก ํŠธ์—”๋“œ๊นŒ์ง€ ํƒ€์ž…์„ ๊ณต์œ (Eden Treaty) ํ•  ์ˆ˜ ์žˆ๋Š” ํƒ€์ž… ์•ˆ์ „ API ํ”„๋ ˆ์ž„์›Œํฌ์ž…๋‹ˆ๋‹ค.


1. ํ•„์ˆ˜ ์‚ฌ์ „ ์š”๊ฑด & ์‹œ์Šคํ…œ ์š”๊ตฌ์‚ฌ์–‘ (Prerequisites & System Requirements)

๐Ÿ’ป ์‹œ์Šคํ…œ ์„ค์น˜ ๋ฐ ์‹คํ–‰ ์š”๊ตฌ์‚ฌ์–‘ (System Requirements)

ํ•ญ๋ชฉ์ตœ์†Œ ์‚ฌ์–‘ (Minimum)๊ถŒ์žฅ ์‚ฌ์–‘ (Recommended)์ฐธ๊ณ  ๋ฐ ๋น„๊ณ 
๋Ÿฐํƒ€์ž„ / ํŒจํ‚ค์ง€Bun v1.4.0+Bun v1.4.5 + ElysiaJS v1.4.32TypeScript 5.6+ ๋„ค์ดํ‹ฐ๋ธŒ ์‹คํ–‰
CPU / RAM1 vCPU / 512 MB RAM2~4 vCPU / 2 GB+ RAM์ดˆ์ €๋ฉ”๋ชจ๋ฆฌยท์ดˆ๊ณ ์† ์ฝœ๋“œ ์Šคํƒ€ํŠธ
# 1. Bun ์ตœ์‹  ๋ฒ„์ „ ์„ค์น˜ (macOS / Linux)
curl -fsSL https://bun.sh/install | bash
# Windows PowerShell: powershell -c "irm bun.sh/install.ps1 | iex"

bun --version
# 1.4.5

# 2. ElysiaJS ํ”„๋กœ์ ํŠธ ์ƒ์„ฑ
bun create elysia elysia-api && cd elysia-api
bun add @elysiajs/openapi @elysiajs/eden

[1๊ฐ•] ElysiaJS ์Šคํ‚ค๋งˆ ๊ฒ€์ฆ(t.Object) & ๋‚ด์žฅ bun:sqlite ์—ฐ๋™

Elysia์˜ t (TypeBox ๊ธฐ๋ฐ˜) ์Šคํ‚ค๋งˆ๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๋Ÿฐํƒ€์ž„ ์œ ํšจ์„ฑ ๊ฒ€์‚ฌ์™€ TypeScript ์ •์  ํƒ€์ž… ์ถ”๋ก , OpenAPI ์ŠคํŽ™์ด ๋‹จ ํ•œ ๋ฒˆ์˜ ์„ ์–ธ์œผ๋กœ ๋™์‹œ์— ์™„์„ฑ๋ฉ๋‹ˆ๋‹ค.

// src/index.ts
import { Elysia, t } from "elysia";
import { Database } from "bun:sqlite";
import { openapi } from "@elysiajs/openapi";

const db = new Database("app.sqlite", { create: true });
db.run(`
  CREATE TABLE IF NOT EXISTS tasks (
    id INTEGER PRIMARY KEY AUTOINCREMENT,
    title TEXT NOT NULL,
    done INTEGER DEFAULT 0
  )
`);

const app = new Elysia()
  .use(openapi()) // http://localhost:3000/openapi ์ž๋™ ๋ฌธ์„œ ์ƒ์„ฑ
  .get("/health", () => ({ ok: true, runtime: `Bun ${Bun.version}` }))
  .get("/tasks", () => db.query("SELECT * FROM tasks ORDER BY id DESC").all())
  .post(
    "/tasks",
    ({ body, set }) => {
      const stmt = db.query("INSERT INTO tasks (title) VALUES (?) RETURNING *");
      const created = stmt.get(body.title);
      set.status = 201;
      return created;
    },
    {
      body: t.Object({
        title: t.String({ minLength: 2, maxLength: 100 }),
      }),
    }
  )
  .listen(3000);

export type App = typeof app;
console.log(`๐ŸฆŠ Elysia running at http://localhost:${app.server?.port}`);

[2๊ฐ•] ์ƒ๋ช…์ฃผ๊ธฐ ํ›…(derive, onError) & ๋ชจ๋“ˆํ˜• ๋ผ์šฐํ„ฐ ๋ถ„๋ฆฌ

์š”์ฒญ ์ปจํ…์ŠคํŠธ์— ์ธ์ฆ๋œ ์‚ฌ์šฉ์ž ์ •๋ณด๋ฅผ ์ฃผ์ž…(derive)ํ•˜๊ณ , ๋„๋ฉ”์ธ ์—๋Ÿฌ๋ฅผ ์ „์—ญ onError ํ•ธ๋“ค๋Ÿฌ๋กœ ๊น”๋”ํ•˜๊ฒŒ ๋ณ€ํ™˜ํ•ฉ๋‹ˆ๋‹ค.

// src/auth-plugin.ts
import { Elysia } from "elysia";

export const authPlugin = new Elysia({ name: "auth-plugin" })
  .derive({ as: "global" }, ({ headers }) => {
    const auth = headers.authorization;
    if (!auth?.startsWith("Bearer ")) {
      return { currentUser: null };
    }
    return { currentUser: { id: "u-101", role: "admin" } };
  })
  .onBeforeHandle(({ currentUser, set, path }) => {
    if (path.startsWith("/admin") && !currentUser) {
      set.status = 401;
      return { error: "UNAUTHORIZED", message: "๋กœ๊ทธ์ธ์ด ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค." };
    }
  });

[3๊ฐ•] Eden Treaty: ์ œ๋กœ ์ฝ”๋“œ์   End-to-End ํƒ€์ž… ์•ˆ์ „ ํด๋ผ์ด์–ธํŠธ

๋ฐฑ์—”๋“œ์—์„œ export type App = typeof app๋งŒ ๋‚ด๋ณด๋‚ด๋ฉด, ํ”„๋ก ํŠธ์—”๋“œ๋‚˜ ๋งˆ์ดํฌ๋กœ์„œ๋น„์Šค ํด๋ผ์ด์–ธํŠธ์—์„œ tRPC์ฒ˜๋Ÿผ ์ž๋™์™„์„ฑ๊ณผ ์ปดํŒŒ์ผ ํƒ€์ž„ ํƒ€์ž… ์ฒดํฌ๋ฅผ ๋ˆ„๋ฆด ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

// client.ts (ํ”„๋ก ํŠธ์—”๋“œ ๋˜๋Š” BFF ์„œ๋น„์Šค)
import { treaty } from "@elysiajs/eden";
import type { App } from "./src/index";

const api = treaty<App>("localhost:3000");

async function main() {
  // POST /tasks์˜ body ํƒ€์ž…({ title: string })์ด ์ž๋™ ์ถ”๋ก ๋จ!
  const { data, error } = await api.tasks.post({
    title: "Elysia Eden Treaty ์‹ค์ „ ๊ฒ€์ฆ",
  });

  if (error) {
    console.error("์š”์ฒญ ์‹คํŒจ:", error.status, error.value);
    return;
  }
  console.log("์ƒ์„ฑ๋œ Task:", data);
}

main();

[4๊ฐ•] Bun ๋„ค์ดํ‹ฐ๋ธŒ WebSocket Pub/Sub ์‹ค์‹œ๊ฐ„ ์ฑ„ํŒ… & ๋‹จ์ผ ๋ฐ”์ด๋„ˆ๋ฆฌ ๋นŒ๋“œ

Elysia๋Š” Bun์˜ ๋‚ด์žฅ uWebSockets ํ˜ธํ™˜ ์—”์ง„์„ ๊ทธ๋Œ€๋กœ ๊ฐ์‹ธ ์ดˆ๋‹น ์ˆ˜์‹ญ๋งŒ ๋ฉ”์‹œ์ง€๋ฅผ ์ฒ˜๋ฆฌํ•˜๋Š” Pub/Sub ํ† ํ”ฝ ๊ตฌ๋…์„ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค.

// src/ws-chat.ts
import { Elysia, t } from "elysia";

export const wsRoutes = new Elysia().ws("/ws/chat/:room", {
  body: t.Object({ sender: t.String(), text: t.String() }),
  open(ws) {
    const room = `room:${ws.data.params.room}`;
    ws.subscribe(room);
    ws.publish(room, { system: true, text: "์ƒˆ ์‚ฌ์šฉ์ž๊ฐ€ ์ž…์žฅํ–ˆ์Šต๋‹ˆ๋‹ค." });
  },
  message(ws, message) {
    const room = `room:${ws.data.params.room}`;
    ws.publish(room, { sender: message.sender, text: message.text });
  },
});
# ํ”„๋กœ๋•์…˜ ๋‹จ์ผ ์‹คํ–‰ ๋ฐ”์ด๋„ˆ๋ฆฌ๋กœ ์ปดํŒŒ์ผ (Docker scratch/distroless ๋ฐฐํฌ ์ตœ์ ํ™”)
bun build --compile --minify --sourcemap ./src/index.ts --outfile server
./server
๐Ÿ’ฌ

์˜๊ฒฌ ๋ฐ ์งˆ๋ฌธ

0
0 / 1500