Elysia in 100 Seconds
Source: Dev.to
Overview
ElysiaJS is a high‑performance, statically typed web framework that runs on the Bun runtime. It is known for being significantly faster than Express and aims to provide a developer‑friendly API while retaining raw speed.
Features
- Speed – Leverages Bun’s Just‑In‑Time compiler for microsecond‑level startup times.
- Type safety – Uses TypeBox to provide end‑to‑end type safety without runtime performance penalties or extensive boilerplate.
- Fluent API – Routes are defined with a chainable syntax (
.get(),.post(), etc.) that returns plain objects automatically serialized to JSON. - Schema validation – Attach a schema object via the
tvariable to validate incoming data at runtime and infer TypeScript types for IDE autocompletion. - Eden Treaty – Allows the frontend to import backend type definitions directly, giving full autocompletion for API calls on the client side.
- Minimal decorators – Unlike NestJS, Elysia does not require complex decorator usage to define routes.
Getting Started
-
Install Bun (if you haven’t already).
-
Create a TypeScript file (e.g.,
app.ts). -
Initialize the app:
import { Elysia, t } from "elysia"; const app = new Elysia(); -
Define a route using the fluent API:
app.get("/", () => { return { message: "Hello, Elysia!" }; }); -
Add schema validation (optional but recommended):
app.get( "/user/:id", { params: t.Object({ id: t.String(), }), }, ({ params }) => { // `params.id` is guaranteed to be a string return { userId: params.id }; } ); -
Run the server:
bun run app.tsThe server will start up in microseconds, ready to handle requests.
Summary
ElysiaJS combines the speed of Bun with robust type safety and a concise, chainable API, making it a compelling alternative to traditional Node.js frameworks like Express and NestJS. Its built‑in schema validation and Eden Treaty further streamline full‑stack development by keeping backend and frontend type definitions in sync.