Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/lullaby6/elysia-file-routing

Bun Package - File router for Elysia.js
https://github.com/lullaby6/elysia-file-routing

bun elysia elysiajs

Last synced: 24 days ago
JSON representation

Bun Package - File router for Elysia.js

Awesome Lists containing this project

README

        

# elysia-file-router

File router for Elysia.js inspired by Next.js, Remix, Astro...

## Install

```bash
bun install elysia-file-routing
```

## Usage

Import and use the plugin ```fileRouter``` in the Elysia app.

By default the value for ```fileRouter``` is ```"./routes/"```.

```js
// src/index.ts
import { Elysia } from "elysia";
import fileRouter from 'elysia-file-router'
import path from "path";

const app = new Elysia()
.use(fileRouter(path.join(__dirname, 'routes')))
.listen(3000);
```

A simple ```GET``` route:

```js
// src/routes/get.ts
export default function () {
return {
hello: 'World'
}
}
```

Using params (path value):

```js
// src/routes/user/[id]/get.ts
export default function ({params: { id }}) {
return {
user_id: id
}
}
```

The file name specifies the request method

for example a simple ```POST``` endpoint:

```js
// src/routes/user/post.ts
export default function () {
return {
message: "Creating User"
}
}
```

## License

MIT