@dataqoi/fastify-router
v0.2.2
Published
A simple and easy-to-use file-based routing system for Fastify.
Downloads
34
Maintainers
Readme
@dataqoi/fastify-router
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
:
- Clone this repository.
- Run
cd test
to navigate to the test project. - Run
npm install
to install the dependencies. - Run
npm start
to start the server. - Play with the API using some kind of client.
Example
A simple fastify server setup with @dataqoi/fastify-router
:
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:
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/:id
GET /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