DEV Community

Cover image for Fastify Autoroutes
Giovanni Cardamone
Giovanni Cardamone

Posted on • Edited on

Fastify Autoroutes

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

📁 GitHub Repository

📰 Full Documentation

🚀 Install

npm install --save fastify-autoroutes
Enter fullscreen mode Exit fullscreen mode

📘 Usage

Register plugin

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

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

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'
  },
})
Enter fullscreen mode Exit fullscreen mode

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!'
  }
}
Enter fullscreen mode Exit fullscreen mode

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}`)
    }
  },
})
Enter fullscreen mode Exit fullscreen mode

â–ļī¸ 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 _ charater 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
Enter fullscreen mode Exit fullscreen mode

⚠ī¸ also any *.test.js and *.test.ts are skipped!

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

📄 License

Licensed under MIT

Top comments (2)

Collapse
 
shair profile image
Shair

Very useful. I have a question, If I am using Javascript without last features, it's possible to use export default instead of module.exports?

Collapse
 
giovannicardamone profile image
Giovanni Cardamone • Edited

'export default' is part of the ES6 module system, so to use it you need a javascript runtime that support it, or you can transpile from highter version to lower using babel.

Anyway the library support both.

Thanks for the question i hope i answered it in a clear way :)