メインコンテンツへスキップ
Fastify

高速で低オーバーヘッドな Node.js 用ウェブフレームワーク

スター   フォーク

なぜ Fastify なのか

効率的なサーバーは、インフラコストの削減、負荷時の応答性の向上、ユーザーの満足度向上を意味します。セキュリティ検証と便利な開発を犠牲にすることなく、可能な限り多くのリクエストを処理しながら、サーバーのリソースを効率的に管理するにはどうすればよいでしょうか?

Fastify の出番です。Fastify は、最小限のオーバーヘッドと強力なプラグインアーキテクチャで、最高の開発者エクスペリエンスを提供することに重点を置いたウェブフレームワークです。Hapi と Express に触発され、我々の知る限りでは、最も高速なウェブフレームワークの一つです。

Fastify を利用している組織

Fastify は、多くの組織や製品のエコシステムを支えています。

スポンサー

Fastify の財政的なスポンサーになりませんか?以下でサポートしてください。 GitHub または Open Collective.

  • Mercedes-Benz Group is using Fastify
  • Handsontable is using Fastify

導入事例

あなたの組織を こちらに掲載しませんか??

  • nullplatform is using Fastify
  • hotstar is using Fastify
  • Codequest is using Fastify
  • Gumlet is using Fastify
  • car2go is using Fastify
  • Seznam.cz is using Fastify
  • Quero Educação is using Fastify
  • The Outnet is using Fastify
... その他多数!

コア機能

これらは、Fastify が構築された主な機能と原則です。

  • 高いパフォーマンス:我々の知る限りでは、Fastify は最も高速なウェブフレームワークの一つであり、コードの複雑さによっては、1秒あたり最大 30,000 件のリクエストを処理できます。
  • 拡張性:Fastify は、フック、プラグイン、デコレーターを介して完全に拡張可能です。
  • スキーマベース:必須ではありませんが、 JSON Schema を使用してルートを検証し、出力をシリアライズすることをお勧めします。内部的には、Fastify がスキーマを非常に高性能な関数にコンパイルします。
  • ロギング:ログは非常に重要ですが、コストがかかります。このコストをほぼ排除するために、最高のロガーである Pino を選択しました!
  • 開発者フレンドリー:このフレームワークは、パフォーマンスとセキュリティを犠牲にすることなく、非常に表現力豊かで、開発者の日常的な使用を支援するように構築されています。
  • TypeScript 対応:私たちは、成長を続ける TypeScript コミュニティをサポートするために、 TypeScript の型宣言ファイルを維持するよう努めています。

クイックスタート

NPM で Fastify を取得

npm install fastify

次に、server.js を作成し、次のコンテンツを追加します。

// Import the framework and instantiate it
import Fastify from 'fastify'
const fastify = Fastify({
logger: true
})

// Declare a route
fastify.get('/', async function handler (request, reply) {
return { hello: 'world' }
})

// Run the server!
try {
await fastify.listen({ port: 3000 })
} catch (err) {
fastify.log.error(err)
process.exit(1)
}

最後に、次のコマンドでサーバーを起動します。

node server

次のコマンドでテストします。

curl http://localhost:3000

CLI の使用

新しいスキャフォールディングプロジェクトを作成するには、 fastify-cli を取得します。

npm install --global fastify-cli
fastify generate myproject

リクエスト/レスポンスの検証とフック

Fastify はこれ以上のことができます。たとえば、JSON Schema を使用して入力と出力の検証を簡単に行い、ハンドラーが実行される前に特定のアクションを実行できます。

import Fastify from 'fastify'
const fastify = Fastify({
logger: true
})

fastify.route({
method: 'GET',
url: '/',
schema: {
// request needs to have a querystring with a `name` parameter
querystring: {
type: 'object',
properties: {
name: { type: 'string'}
},
required: ['name'],
},
// the response needs to be an object with an `hello` property of type 'string'
response: {
200: {
type: 'object',
properties: {
hello: { type: 'string' }
}
}
}
},
// this function is executed for every request before the handler is executed
preHandler: async (request, reply) => {
// E.g. check authentication
},
handler: async (request, reply) => {
return { hello: 'world' }
}
})

try {
await fastify.listen({ port: 3000 })
} catch (err) {
fastify.log.error(err)
process.exit(1)
}

TypeScript サポート

Fastify には型定義ファイルが同梱されていますが、使用している Node.js のバージョンによっては、@types/node をインストールする必要がある場合があります。
次の例では、HTTP サーバーを作成します。
使用する HTTP バージョンに関連する型定義を渡します。型を渡すことで、ルート内の基になる HTTP オブジェクトに正しい型でアクセスできます。
http2 を使用する場合は、次のように渡します。 <http2.Http2Server, http2.Http2ServerRequest, http2.Http2ServerResponse>.
HTTPS の場合は、Server の代わりに http2.Http2SecureServer または http.SecureServer を渡します。
これにより、サーバーハンドラー内でも、次の適切な型定義で http.ServerResponse を取得できます。 reply.res.

import Fastify, { FastifyInstance, RouteShorthandOptions } from 'fastify'
import { Server, IncomingMessage, ServerResponse } from 'http'

const server: FastifyInstance = Fastify({})

const opts: RouteShorthandOptions = {
schema: {
response: {
200: {
type: 'object',
properties: {
pong: {
type: 'string'
}
}
}
}
}
}

server.get('/ping', opts, async (request, reply) => {
return { pong: 'it worked!' }
})

const start = async () => {
try {
await server.listen({ port: 3000 })

const address = server.server.address()
const port = typeof address === 'string' ? address : address?.port

} catch (err) {
server.log.error(err)
process.exit(1)
}
}

start()

ドキュメント にアクセスして、Fastify が提供するすべての機能について詳しく学んでください。

高速なウェブフレームワーク

Node.js のパフォーマンスに関する経験を活用し、Fastify は 可能な限り高速になるようにゼロから構築されています。他の一般的なウェブフレームワークと比較した Fastify のパフォーマンスについては、 ベンチマークセクションをご覧ください。

ベンチマークをチェックする

エコシステム

Fastify には、プラグインのエコシステムが拡大し続けています。お気に入りのデータベースやテンプレート言語用のプラグインがすでにあるかもしれません。現在利用可能なプラグインをナビゲートするには、 エコシステムページをご覧ください。お探しのプラグインが見つかりませんか?問題ありません。 自分で書くのは簡単です!

プラグインを299探索する

チームの紹介

アルファベット順

リードメンテナー

Matteo Collina's profile picture

Matteo Collina

Tomas Della Vedova's profile picture

Tomas Della Vedova

Manuel Spigolon's profile picture

Manuel Spigolon

James Sumners's profile picture

James Sumners

協力者

Aras Abbasi's profile picture

Aras Abbasi

Tommaso Allevi's profile picture

Tommaso Allevi

Ayoub El Khattabi's profile picture

Ayoub El Khattabi

David Clements's profile picture

David Clements

Dan Castilo's profile picture

Dan Castilo

Gürgün Dayıoğlu's profile picture

Gürgün Dayıoğlu

Dustin Deus's profile picture

Dustin Deus

Carlos Fuentes's profile picture

Carlos Fuentes

Rafael Gonzaga's profile picture

Rafael Gonzaga

Jean Michelet's profile picture

Jean Michelet

Vincent Le Goff's profile picture

Vincent Le Goff

Luciano Mammino's profile picture

Luciano Mammino

Salman Mitha's profile picture

Salman Mitha

Igor Savin's profile picture

Igor Savin

Evan Shortiss's profile picture

Evan Shortiss

Maksim Sinik's profile picture

Maksim Sinik

Frazer Smith's profile picture

Frazer Smith

過去の協力者

Ethan Arrowood's profile picture

Ethan Arrowood

Çağatay Çalı's profile picture

Çağatay Çalı

Cemre Mengu's profile picture

Cemre Mengu

Trivikram Kamat's profile picture

Trivikram Kamat

Nathan Woltman's profile picture

Nathan Woltman

謝辞

このプロジェクトは、

からスポンサー提供を受けています

素晴らしい Fastify コミュニティにも感謝します。

主催

私たちは、 OpenJS Foundation

OpenJS Logo