https://github.com/node-ecosystem/universal-autorouter-hono
Universal filesystem autoloader for Hono routes
https://github.com/node-ecosystem/universal-autorouter-hono
api api-rest autoloader bun express-js expressjs filesystem hmr hono nodejs routes vike vite web-server
Last synced: about 1 month ago
JSON representation
Universal filesystem autoloader for Hono routes
- Host: GitHub
- URL: https://github.com/node-ecosystem/universal-autorouter-hono
- Owner: node-ecosystem
- License: mit
- Created: 2025-01-11T14:17:25.000Z (about 1 year ago)
- Default Branch: master
- Last Pushed: 2025-01-13T22:58:08.000Z (about 1 year ago)
- Last Synced: 2025-08-28T22:03:40.047Z (7 months ago)
- Topics: api, api-rest, autoloader, bun, express-js, expressjs, filesystem, hmr, hono, nodejs, routes, vike, vite, web-server
- Language: TypeScript
- Homepage:
- Size: 337 KB
- Stars: 2
- Watchers: 0
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# universal-autorouter-hono
An plugin for [Hono](https://hono.dev) to add HMR (Hot Module Replacement) to Hono routes development.
Package required:
- [universal-autorouter](https://github.com/node-ecosystem/universal-autorouter), used under the hood to autoload routes
- [Vite](https://vite.dev), needed for _ViteDevServer instance_
## ⚙️ Install
```sh
yarn add -D universal-autorouter-hono
```
## 📖 Usage
### Register the Plugin
```ts
// /app.ts
import path from 'node:path'
import { serve } from '@hono/node-server'
import { Hono } from 'hono'
import type { AutoloadRoutesOptions } from 'universal-autorouter'
const app = new Hono()
let autoloadRoutes
// Options of "universal-autorouter" or "universal-autorouter-hono" package
const autoloadRoutesOptions: AutoloadRoutesOptions = {
// Pattern to scan route files
pattern: '**/*.ts',
// Prefix to add to routes
prefix: '/api',
// Source directory for route handler files
routesDir: path.resolve(import.meta.dirname, 'api'),
// Directories for files used by route handler files (as default scan all files)
externalDirs: ['utils']
}
if (process.env.NODE_ENV === 'production') {
({ default: autoloadRoutes } = await import('universal-autorouter'))
autoloadRoutesOptions.pattern = '**/*.mjs'
} else {
({ default: autoloadRoutes } = await import('universal-autorouter-hono'))
autoloadRoutesOptions.pattern = '**/*.ts'
autoloadRoutesOptions.viteDevServer = ''
// Example with Vike package
// autoloadRoutesOptions.viteDevServer = globalThis.__vikeNode!.viteDevServer
}
await autoloadRoutes(app, autoloadRoutesOptions)
const port = +(process.env.PORT || 3000)
serve({
fetch: app.fetch,
port
}, () => console.log(`Server running at http://localhost:${port}`))
```
## License
This project is licensed under the [MIT License](LICENSE).