fastify
Web framework for node.js
v3 は 2023/7 EOF
Migration-Guide-V4
Fastify v4 GA. After two years we are excited to… | by Fastify | Medium
Fastify DX というフルスタックなフレームワークは伸びるか?
routing
ファイルに分割したい
plugin として handler を定義して register する形が一般的にか?
plugin
Routes
TS
fastify/types at main · fastify/fastify
fastifyでハンドラに型付けする方法 | DevelopersIO
Directory structure
https://github.com/fastify/help/issues/76
Error
Hooks
Errors
デフォルトのエラーハンドラーに任せる方法がある
カスタマイズする場合は setErrorHandler を定義することが可能
Error handlers are fully encapsulated, so a setErrorHandler call within a plugin will limit the error handler to that plugin's context.
どこまで影響を与えるかは fastify の context のルールにしたがう
Encapsulation
Logging
fastify instance の logger を使いたいので、サーバーの初期化は index とは別の server.ts とかで行って、log も server.ts で export するのがよさそう
Test
https://www.fastify.io/docs/latest/Guides/Testing/
API を呼び出す部分を、外部ライブラリに頼る必要がない
サーバーを起動しなくてもテストできる
Reply.send() が複数実行される可能性がある場合どのように書くべきか
void を return するべき
code:shell
if (error instanceof GenericError) {
reply.status(error.statusCode).send({ code: error.code, message: error.message });
return; // 当然 return しなければ、下の send まで実行される。ただし、fastify が reseponse が送られているかどうかを管理してくれているので、Reply was already sent というエラーになるのでクライアント側に問題が起きることはない。
}
request.log.fatal(error, error.message);
reply.status(500).send({ code: 'UNKNOWN', message: 'Unknow error occured' });
Routes async か通常かについて