Plugin
모든 Elysia 인스턴스는 use 메서드를 통해 다른 인스턴스와 플러그 앤 플레이할 수 있습니다.
typescript
import { Elysia } from 'elysia'
const user = new Elysia()
.get('/profile', 'User Profile')
.get('/settings', 'User Settings')
new Elysia()
.use(user)
.get('/', 'Home')
.listen(3000)적용되면 user 인스턴스의 모든 route가 app 인스턴스에서 사용 가능합니다.
Plugin Config
인수를 받아 Elysia 인스턴스를 반환하는 플러그인을 만들어 더 동적인 플러그인을 만들 수도 있습니다.
typescript
import { Elysia } from 'elysia'
const user = ({ log = false }) => new Elysia()
.onBeforeHandle(({ request }) => {
if (log) console.log(request)
})
.get('/profile', 'User Profile')
.get('/settings', 'User Settings')
new Elysia()
.use(user({ log: true }))
.get('/', 'Home')
.listen(3000)과제
user 인스턴스를 app 인스턴스에 적용해 봅시다.
Show answer
위의 예제와 유사하게, use 메서드를 사용하여 user 인스턴스를 app 인스턴스에 연결할 수 있습니다.
typescript
import { Elysia } from 'elysia'
new Elysia()
.get('/profile', 'User Profile')
.get('/settings', 'User Settings')
const app = new Elysia()
.use(user)
.get('/', 'Home')
.listen(3000)