Skip to content

Fastify チートシート

オーバーヘッドの少ない高速な Node.js Web フレームワーク。

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);

プラグイン

プレフィックス付きでカプセル化されたプラグイン。

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

Was this helpful?