なぜ Fastify なのか
効率的なサーバーは、インフラコストの削減、負荷時の応答性の向上、ユーザーの満足度向上を意味します。セキュリティ検証と便利な開発を犠牲にすることなく、可能な限り多くのリクエストを処理しながら、サーバーのリソースを効率的に管理するにはどうすればよいでしょうか?
Fastify の出番です。Fastify は、最小限のオーバーヘッドと強力なプラグインアーキテクチャで、最高の開発者エクスペリエンスを提供することに重点を置いたウェブフレームワークです。Hapi と Express に触発され、我々の知る限りでは、最も高速なウェブフレームワークの一つです。
Fastify を利用している組織
Fastify は、多くの組織や製品のエコシステムを支えています。
コア機能
これらは、Fastify が構築された主な機能と原則です。
- 高いパフォーマンス:我々の知る限りでは、Fastify は最も高速なウェブフレームワークの一つであり、コードの複雑さによっては、1秒あたり最大 30,000 件のリクエストを処理できます。
- 拡張性:Fastify は、フック、プラグイン、デコレーターを介して完全に拡張可能です。
- スキーマベース:必須ではありませんが、 JSON Schema を使用してルートを検証し、出力をシリアライズすることをお勧めします。内部的には、Fastify がスキーマを非常に高性能な関数にコンパイルします。
- ロギング:ログは非常に重要ですが、コストがかかります。このコストをほぼ排除するために、最高のロガーである Pino を選択しました!
- 開発者フレンドリー:このフレームワークは、パフォーマンスとセキュリティを犠牲にすることなく、非常に表現力豊かで、開発者の日常的な使用を支援するように構築されています。
- TypeScript 対応:私たちは、成長を続ける TypeScript コミュニティをサポートするために、 TypeScript の型宣言ファイルを維持するよう努めています。
クイックスタート
NPM で Fastify を取得
npm install fastify
次に、server.js
を作成し、次のコンテンツを追加します。
- ESM
- CJS
// 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)
}
// Require the framework and instantiate it
const fastify = require('fastify')({ logger: true })
// Declare a route
fastify.get('/', function handler (request, reply) {
reply.send({ hello: 'world' })
})
// Run the server!
fastify.listen({ port: 3000 }, (err) => {
if (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 を使用して入力と出力の検証を簡単に行い、ハンドラーが実行される前に特定のアクションを実行できます。
- ESM
- CJS
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)
}
const fastify = require('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: (request, reply, done) => {
// E.g. check authentication
done()
},
handler: (request, reply) => {
reply.send({ hello: 'world' })
}
})
fastify.listen({ port: 3000 }, (err) => {
if (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
.
- TypeScript
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 には、プラグインのエコシステムが拡大し続けています。お気に入りのデータベースやテンプレート言語用のプラグインがすでにあるかもしれません。現在利用可能なプラグインをナビゲートするには、 エコシステムページをご覧ください。お探しのプラグインが見つかりませんか?問題ありません。 自分で書くのは簡単です!
チームの紹介
アルファベット順