iT邦幫忙

2022 iThome 鐵人賽

DAY 7
0
Modern Web

Fastify 101系列 第 7

[Fastify] Day07 - FastifyInstance and FastifyServerOptions

  • 分享至 

  • xImage
  •  

大家好,我是 Yubin

這篇文章介紹 Fastify server 在程式中的實體 (instance),Type 定義在 FastifyInstance

FastifyInstance

fastify 模組 default export 的是一個用來產生 FastifyInstance 的 function,可以利用那個 function 來產生 FastifyInstance 物件。

import fastify, { FastifyInstance } from 'fastify'

const server: FastifyInstance = fastify()

用這種方式就可以產生一個 FastifyInstance 出來,就可以開始定義 route,開發 API。

這個 fastify() 的 function 可以帶入一個 FastifyServerOptions 物件來設定這個 server,而不是全部走 default 值。

import fastify, { FastifyInstance, FastifyServerOptions } from 'fastify'

const serverOptions : FastifyServerOptions = {
    logger: true,
    // ...
}

const server: FastifyInstance = fastify(serverOptions)

FastifyServerOptions

  • http2
    預設為 false
    如果設定為 true,則使用 HTTP/2 模組來建立 socket。
  • connectionTimeout
    預設為 0 (不設定逾時)。
    以毫秒(ms)為單位定義 server 的 timeout。可以參考 NodeJS http 模組中的 server.timeout
  • keepAliveTimeout
    預設為 72000 ms (72秒)。
    設定 keep-alive 的 timeout 時間。詳細可以參考 NodeJS http 模組中的 server.keepAliveTimeout
  • maxRequestsPerSocket
    預設為 0 (沒有限制)。
    定義一個 keep alive 的連接中最大可以接受的 request 個數。詳細參考 NodeJS http 模組中的 server.maxRequestsPerSocket
  • requestTimeout
    預設為 0 (沒有限制)。
    定義一個 request 多久後 timeout。詳細參考 NodeJS http 模組中的 server.requestTimeout
  • ignoreTrailingSlash
    預設為 false,不忽略結尾斜線。
    也就是 /hello/hello/ 代表不同 Endpoint。
  • ignoreDuplicateSlashes
    預設為 false
    若設為 true,則 /api/hello/api//hello 代表同一個 Endpoint。
    如果上游的 rewrite 機制或什麼奇怪的原因,導致開頭多一個斜線的時候非常好用。
  • maxParamLength
    預設為 100,定義 route 中參數的最大長度。
    防範一些網路攻擊很有效。
  • bodyLimit
    預測為 1048576 (1MiB)。
    定義 Payload 是最大上限。
  • logger
    預設為 false
    設為 true,則啟用內建的 pino 做為 server 的 logger。
  • disableRequestLogging
    預設為 false
    當 logger 為 true,預設情況下,每個 request 進來、每個 response 出去都會寫 log。
    設為 false 就不會幫你寫這兩個預設的 log。
  • serverFactory
    可以傳入 http server 進來,初始化 Fastify server。
  • caseSensitive
    預設為 true
    大小寫敏感,也就是 /hello/HELLO 是不同的 Endpoint。
    若設為 false 會違反 RFC3986
  • requestIdHeader
    預設為 request-id。參考 logging-request-id
    每個 request 進來都會給一個 id,若設為 false,則 id 產生的方式由 genReqId 決定。
  • requestIdLogLabel
    預設為 reqId
    定義寫進 log 時候,request id 的欄位名稱。
  • genReqId
    預設為 request-id 的 handler。
    傳入一個 function,可以自己定義 request id 要怎麼產生。
  • trustProxy
    預設為 false
    若設為 true,fastify server 會信任 X-Forwarded-* 的 HTTP Header。
  • pluginTimeout
    預設為 10000 ms (10秒)。
    定義註冊 plugin 的 timeout。
  • return503OnClosing
    預設為 true
    Server 在 close 被呼叫的時候,若有 request 進來則回應 503 的狀態碼。
  • rewriteUrl
    可以對 req 物件的 url 屬性進行 rewrite。
    傳入一個 function,修改 request 物件中 url 的值。
function rewriteUrl (req) { // req is the Node.js HTTP request
  return req.url === '/hi' ? '/hello' : req.url;
}

以上更詳細的參數跟介紹可以參考 FastifyServerOptions


上一篇
[Fastify] Day06 - Route
下一篇
[Fastify] Day08 - Fastify Server Methods
系列文
Fastify 10130
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

1 則留言

0
Brandy
iT邦新手 3 級 ‧ 2022-09-23 20:22:44

極限發文

我要留言

立即登入留言