fastify-router

A simple and easy-to-use file-based routing system for Fastify.

Downloads
483
Stars
0

@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:

  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:

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

Package Rankings
Top 31.58% on Npmjs.org
Badges
Extracted from project README's
CI NPM version js-standard-style