Cheat Sheet

Ping Pong

import { Elysia } from "elysia";

const app = new Elysia().get("/ping", () => "pong").listen(8080);

console.log(`🦊 Elysia is running at http://${app.server?.hostname}:${app.server?.port}`);

Custom Method

import { Elysia } from "elysia";

new Elysia()
  .get("/hi", () => "Hi")
  .post("/hi", () => "From Post")
  .put("/hi", () => "From Put")
  .route("M-SEARCH", () => "Custom Method")
  .listen(8080);

Path Params

import { Elysia } from "elysia";

new Elysia()
  .get("/id/:id", ({ params: { id } }) => id)
  .get("/rest/*", () => "Rest")
  .listen(8080);

Return JSON

import { Elysia } from "elysia";

new Elysia()
  .get("/json", () => ({
    hi: "Elysia",
  }))
  .listen(8080);

Header and status code

import { Elysia } from "elysia";

new Elysia()
  .get("/", ({ set }) => {
    set.status = 418;
    set.headers["x-powered-by"] = "Elysia";

    return "I'm teapod";
  })
  .listen(8080);

Group

import { Elysia } from "elysia";

new Elysia()
  .get("/", () => "Hi")
  .group("/auth", app => {
    return app
      .get("/", () => "Hi")
      .post("/sign-in", ({ body }) => body)
      .put("/sign-up", ({ body }) => body);
  })
  .listen(8080);

Hook and Schema

import { Elysia, t } from "elysia";

new Elysia()
  .onRequest(() => {
    console.log("On request");
  })
  .on("beforeHandle", () => {
    console.log("Before handle");
  })
  .post("/mirror", ({ body }) => body, {
    body: t.Object({
      username: t.String(),
      password: t.String(),
    }),
    afterHandle: () => {
      console.log("After handle");
    },
  })
  .listen(8080);

Guard

import { Elysia, t } from "elysia";

new Elysia()
  .guard(
    {
      response: t.String(),
    },
    app =>
      app
        .get("/", () => "Hi")
        // Invalid: will throws error, and TypeScript will report error
        .get("/invalid", () => 1)
  )
  .listen(8080);

State and Decorate

import { Elysia } from "elysia";

new Elysia()
  .state("version", 1)
  .decorate("getDate", () => Date.now())
  .get("/version", ({ getDate, store: { version } }) => `${version} ${getDate()}`)
  .listen(8080);

Redirect

import { Elysia } from "elysia";

new Elysia()
  .get("/", () => "hi")
  .get("/redirect", ({ set }) => {
    set.redirect = "/";
  })
  .listen(8080);

Plugin

import { Elysia } from "elysia";

const plugin = new Elysia().state("plugin-version", 1).get("/hi", () => "hi");

new Elysia()
  .use(plugin)
  .get("/version", ({ store }) => store["plugin-version"])
  .listen(8080);