Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/dataqoi/fastify-router
A simple and easy-to-use file-based routing system for Fastify.
https://github.com/dataqoi/fastify-router
fastify fastify-plugin npm router
Last synced: 2 months ago
JSON representation
A simple and easy-to-use file-based routing system for Fastify.
- Host: GitHub
- URL: https://github.com/dataqoi/fastify-router
- Owner: dataqoi
- Created: 2024-09-20T05:05:37.000Z (3 months ago)
- Default Branch: main
- Last Pushed: 2024-09-26T22:19:28.000Z (3 months ago)
- Last Synced: 2024-09-30T10:42:07.700Z (3 months ago)
- Topics: fastify, fastify-plugin, npm, router
- Language: TypeScript
- Homepage: https://www.npmjs.com/package/@dataqoi/fastify-router
- Size: 2.05 MB
- Stars: 0
- Watchers: 0
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# @dataqoi/fastify-router
[![CI](https://github.com/dataqoi/fastify-router/actions/workflows/deploy.yml/badge.svg)](https://github.com/dataqoi/fastify-router/actions)
[![NPM version](https://img.shields.io/npm/v/@dataqoi/fastify-router.svg?style=flat)](https://www.npmjs.com/package/@dataqoi/fastify-router)
[![js-standard-style](https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat)](https://standardjs.com/)A simple and easy-to-use file-based routing system for Fastify.
## Installation
```
npm i @dataqoi/fastify-router
```## Configuration
Autoload can be customised using the following options:
- `version` (optional) - If set, any route with the version prefix will be duplicated and loaded as default routes.
- `routesBasePath` (optional) - Default: "routes". The base directory to load routes from.
## Test Project
If you would like to see a sample API project using `@dataqoi/fastify-router`:
1. Clone this repository.
2. Run `cd test` to navigate to the test project.
3. Run `npm install` to install the dependencies.
4. Run `npm start` to start the server.
5. Play with the API using some kind of client.## Example
A simple fastify server setup with `@dataqoi/fastify-router`:
```js
const fastify = require('fastify')
const router = require('@dataqoi/fastify-router')const app = fastify()
app.register(router, {
version: 'v1', // optional
routerBasePath: 'routes', // optional
})app.listen({ port: 3000 })
```or with ESM syntax:
```js
import fastify from 'fastify'
import router from '@dataqoi/fastify-router'const app = fastify()
app.register(router, {
version: 'v1', // optional
routerBasePath: 'routes', // optional
})app.listen({ port: 3000 })
```Folder structure:
```
├── routes
│ ├── v1
│ │ ├── versions.js
│ │ └── users
│ │ ├── [id]
│ │ │ └── index.js
│ │ ├── index.js
│ │ └── list.js
│ └── v2
│ ├── versions.js
│ └── users
│ ├── [id]
│ │ └── index.js
│ ├── index.js
│ └── list.js
├── package.json
└── app.js
```Resulting routes:
```
GET /v1/versions
GET /v1/users
GET /v1/users/list
GET /v1/users/:idGET /v2/versions
GET /v2/users
GET /v2/users/list
GET /v2/users/:id# if version = 'v1' or 'v2'
GET /versions
GET /users
GET /users/list
GET /users/:id
```## License
MIT