Automatic Routes #928
DIEGOHORVATTI
started this conversation in
Ideas
Replies: 2 comments 1 reply
-
This has worked perfectly for me, I recommend this use import Elysia from 'elysia'
import { readdirSync } from 'fs'
import { join } from 'path'
export const router = async (dirname: string) => {
const app = new Elysia({ prefix: '' })
const directory = join(dirname, 'routes')
for (const file of readdirSync(directory)) {
const route: Elysia = (await import(join(directory, file))).default
app.use(route)
console.log(`Loaded route ${file.replace('.routes.ts', '')}`)
}
return app
} import { Elysia, ValidationError } from 'elysia'
import { openApi } from '@/middlewares/openApi'
import { router } from './router'
import { PORT } from '@/constants/config'
new Elysia()
.use(openApi)
.onError(({ code, error }) => {
if (code === 'NOT_FOUND') {
return 'Route not found 😭'
}
if (error instanceof ValidationError) {
return error.message
}
return error
})
.use(router(__dirname))
.get('/', () => 'API is running 🚀')
.listen(PORT, ({ url }) => {
console.log(`🦊 Elysia is running at ${url}`)
}) |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Firstly I love you @SaltyAom ❦
Following this structure is a possible little hack to avoid having to import each route manually
It is important to note that every .routes.js file must be a default export
Beta Was this translation helpful? Give feedback.
All reactions