Plugin
Every Elysia instance can be plug-and-play with other instances using the use method.
import { Elysia } from 'elysia'
const user = new Elysia()
.get('/profile', 'User Profile')
.get('/settings', 'User Settings')
Once applied, all routes from user instance will be available in app instance.
Plugin config
You can also create a plugin that takes an argument and returns an Elysia instance to make a more dynamic plugin.
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')
.use(user({ log: true }))
It’s also recommended that you read about Key Concept: Dependency to understand how Elysia handles dependencies between plugins.
Assignment
Let’s apply the user instance to the app instance.