1. Typescript와 호환이 좋다?
2. express보다 지원하는 기능이 많다?? (플러그인, 데코레이터, 타입 체킹 등)
3. 자유로운 개발 가능?
4. 익스프레스와 마찬가지로 micro frame work라 불림 (express보다 더 많은 것을 제공)
5. 성능이 좋음 (전체 플러그인 캡슐화 한 것으로 Json을 자동 분석해 빠르다? https://www.educative.io/answers/fastify-vs-express)
6. async await가 정식 지원된다. (모든 코어가 비동기식으로 구현?)
7. 자체적으로 로그 수집이 포함
8. 데코레이터, 플러그인 등을 지원하여 확장성이 좋음
공식 사이트 장점
1. 최소한의 오버헤드와 강력한 플러그인 아키텍처
2. 성능이 좋음
플러그인 구조
import Fastify from 'fastify'
const fastify = Fastify()
fastify
.register(function pluginA(fastify, options, done) {
// Add a random number to fastify instance
fastify.decorate('rand', Math.random())
console.log(fastify.rand) // Accessible here
done()
})
.register(function pluginB(fastify, options, done) {
// Try to access the random number added in pluginA
console.log(fastify.rand) // undefined
done()
})
플러그인 구조를 이용하여 캡슐화 혹은 상속
하나의 루트 컨텍스트만 있고 모든 플러그인은 그 하위입니다.
상위 컨텍스트는 하위 컨텍스트의 상태에 액세스할 수 없습니다.
하위 컨텍스트는 상위 컨텍스트의 상태에 액세스할 수 있습니다.
플러그인 구조 장점
소스 코드를 플러그인 단위로 분할 가능
다른 설정으로 플러그인 로드(예: 플러그인을 두 번 등록하고 연결 URL을 변경하여 많은 MongoDB 연결에 연결)
제한된 경로 집합에 후크 추가: 캡슐화 컨텍스트에 정의된 경로??
plugins : 인증과 같이 전체 애플리케이션에서 공유해하는 기능 (인터셉터, cros, 인증, register 등)
routes : 리다이렉션 코드 및 관리 API와 같은 모든 비즈니스 로직이 포함
확인 할 내용 addHook, preHandler// hook 관련 기능
플러그인 구조 확인
설치한 모든 플러그은 fastify-plugin 모듈로 래핑됨, 그렇지 않으면 하위 컨텍스트에 접근할 수 없음
import Fastify from 'fastify'
import fp from 'fastify-plugin'
const fastify = Fastify()
fastify
// Register pluginA with fastify-plugin
.register(fp(function pluginA(fastify, options, done) {
// Add a random number to fastify instance
fastify.decorate('rand', Math.random())
console.log(fastify.rand) // Accessible here
done()
}))
.register(function pluginB(fastify, options, done) {
// Try to access the random number added in pluginA
console.log(fastify.rand) // Also accessible here
done()
})
fastify-overview : 플러그인 구조(tree structure)를 가시적으로 확인 할 수 있는 플러그인
플러그인 load 순서
1. fastify ecosystem flugin
2. custom plugin
3. decorators
4. hooks
5. service
'Back-End' 카테고리의 다른 글
Fastify 설치 및 실행 (0) | 2023.03.10 |
---|