Skip to content

Fastify 치트시트

오버헤드가 매우 낮은 고성능 Node.js 웹 프레임워크입니다.

01

시작하기

Hello 서버

Fastify는 JSON 응답을 자동으로 직렬화합니다.

fastify
import Fastify from "fastify";
const fastify = Fastify();

fastify.get("/", async () => {
  return { hello: "world" };
});

fastify.listen({ port: 3000 });

스키마 검증

빠른 검증을 위해 JSON Schema를 사용합니다.

fastify
fastify.post("/user", {
  schema: {
    body: {
      type: "object",
      required: ["name"],
      properties: {
        name: { type: "string" }
      }
    }
  }
}, async (req) => req.body);

플러그인

접두어(prefix)와 함께 캡슐화된 플러그인을 제공합니다.

fastify
fastify.register(async (instance) => {
  instance.get("/plugin", async () => "ok");
}, { prefix: "/api" });

Was this helpful?