⭐ Thanks everyone who want to star the project, it means a lot!
Automatic add routes based on file system hierarchy.
npm install --save fastify-autoroutes
const fastify = require('fastify')
const server = fastify()
server.register(require('fastify-autoroutes'), {
dir: './<autoroutes-directory>', // relative to your cwd
})
//file: `<autoroutes-directory>/some/route/index.js`
//mapped to: `<your host>/some/route`
export default (fastifyInstance) => ({
get: {
// any of routes option allowed by fastify: https://www.fastify.io/docs/latest/Routes/#routes-option
// except for `url` and `method`
handler: (request, reply) => {
reply.send('hello index route')
}
},
// other available methods are following: ['delete', 'get', 'head', 'patch', 'post', 'put', 'options']
})
ℹ️ use syntax
:paramName
or{paramName}
in file name to specify url parameters
//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}`)
}
},
})
- delete
- get
- head
- patch
- post
- put
- options
to use url parameters in your route use {parmName}
in your file or directory, it will be automatically changed to fastify parameter
to skip file in routes directory, prepend the .
or _
charater to filename
examples:
.skipped_directory
_also_skipped_directory
.skipped_file.js
.skipped_file.ts
_also_skipped_file.js
_also_skipped_file.ts
⚠️ 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
Check CHANGELOG
Licensed under MIT
Thanks goes to these wonderful people (emoji key):
If you want to contribute remember to comment on an issue or pull request with:
@all-contributors please add @jakebolam for infrastructure, tests and code
Giovanni Cardamone 💻 📖 💡 🚧 |
This project follows the all-contributors specification. Contributions of any kind welcome!