fa

fastify-autoroutes

fastest way to map directories to URLs in fastify

Showing:

Popularity

Downloads/wk

41

GitHub Stars

30

Maintenance

Last Commit

1mo ago

Contributors

5

Package

Dependencies

2

License

MIT

Type Definitions

Tree-Shakeable

No?

Categories

Readme

fastify-autoroutes

Logo

JavaScript   TypeScript

NPM version NPM downloads Known Vulnerabilities GitHub license

CI Coverage Status

⭐ Thanks to everyone who has starred the project, it means a lot!

Plugin to handle routes in fastify automatically based on directory structure.

📰 Full Documentation

fastify-autoroutes

🚀 Install

npm install --save fastify-autoroutes

📘 Usage

Register plugin

const fastify = require('fastify')
const server = fastify()

server.register(require('fastify-autoroutes'), {
  dir: './<autoroutes-directory>', // relative to your cwd
})

Create file in autoroutes directory

//file: `<autoroutes-directory>/some/route.js`
//url:  `http://your-host/some/route`

export default (fastifyInstance) => ({
  get: {
    handler: async (request, reply) => 'Hello, Route'
  },
})

Using typescript support for module

//file: `<autoroutes-directory>/some/route.ts`
//url:  `http://your-host/some/route`

import { FastifyInstance } from 'fastify'
import { Resource } from 'fastify-autoroutes'

export default (fastify: FastifyInstance) => <Resource> {
  get: {
    handler: async (request: FastifyRequest, reply: FastifyReply) => 'Hello, Route!'
  }
}

Accepts params in autoroutes

ℹ️ file/directory name must follow syntax :paramName or {paramName}

//file: `<autoroutes-directory>/users/{userId}/photos.js`
//mapped to: `<your host>/users/:userId/photos`

export default (fastifyInstance) => ({
  get: {
    handler: (request, reply) => {
      reply.send(`photos of user ${request.params.userId}`)
    }
  },
})

▶️ Route module definition

Method specification for attributes is available here: Method specification

ℹ️ attributes url and method are dynamically provided

Allowed attributes mapped to Http methods in module:

  • delete
  • get
  • head
  • patch
  • post
  • put
  • options

▶️ Skipping files

to skip file in routes directory, prepend the . or _ character to filename

examples:

routes
├── .ignored-directory
├── _ignored-directory
├── .ignored-js-file.js
├── _ignored-js-file.js
├── .ignored-ts-file.ts
├── _ignored-ts-file.ts
├── ignored-js-test.test.js
└── ignored-ts-test.test.ts

⚠️ also any *.test.js and *.test.ts are skipped!

this is useful if you want to have a lib file which contains functions that don't have to be a route, so just create the file with _ prepending character

📄 License

Licensed under MIT

✨ Contributors

Thanks goes to these wonderful people (emoji key):


Giovanni Cardamone

💻 📖 💡 🚧

Gennaro

🎨

This project follows the all-contributors specification.

Contributions of any kind welcome!

Rate & Review

Great Documentation0
Easy to Use0
Performant0
Highly Customizable0
Bleeding Edge0
Responsive Maintainers0
Poor Documentation0
Hard to Use0
Slow0
Buggy0
Abandoned0
Unwelcoming Community0
100