https://github.com/flaviodelgrosso/fastify-api-gateway
A Fastify plugin for creating an API Gateway with route configuration, middlewares, rate limit and URL prefix rewriting.
https://github.com/flaviodelgrosso/fastify-api-gateway
api-gateway fastify fastify-plugin nodejs
Last synced: 8 months ago
JSON representation
A Fastify plugin for creating an API Gateway with route configuration, middlewares, rate limit and URL prefix rewriting.
- Host: GitHub
- URL: https://github.com/flaviodelgrosso/fastify-api-gateway
- Owner: flaviodelgrosso
- License: isc
- Created: 2024-12-12T09:40:56.000Z (about 1 year ago)
- Default Branch: master
- Last Pushed: 2025-04-07T09:27:59.000Z (8 months ago)
- Last Synced: 2025-04-07T10:29:33.474Z (8 months ago)
- Topics: api-gateway, fastify, fastify-plugin, nodejs
- Language: JavaScript
- Homepage: https://www.npmjs.com/package/fastify-api-gateway
- Size: 1.11 MB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# Fastify API Gateway
[](https://www.npmjs.com/package/fastify-api-gateway)
[](https://www.npmjs.com/package/fastify-api-gateway)
[](https://github.com/flaviodelgrosso/fastify-api-gateway/actions/workflows/ci.yml)
[](https://codecov.io/github/flaviodelgrosso/fastify-api-gateway)
This library is a Fastify plugin that can be used as API Gateway that proxies requests to different backend services. It supports route-level and global middlewares, rate limits, and allows for URL prefix rewriting.
## Install
```bash
npm install fastify-api-gateway
```
### Compatibility
| Plugin version | Fastify version |
| ---------------|-----------------|
| `^1.x` | `^5.x` |
## Configuration
When registering the plugin it will register under the hood the following Fastify plugins:
- [fastify-reply-from](https://github.com/fastify/fastify-reply-from) plugin, which is used to proxy requests to the backend services.
Optionally, it can also register the following plugins:
- [fastify-rate-limit](https://github.com/fastify/fastify-rate-limit) plugin, which is used to rate limit requests. It is enabled only if `rateLimitOptions` is set.
- [fastify-middie](https://github.com/fastify/middie) plugin, which is used to apply middlewares on routes. It is enabled only if `middlewares` are found in the configuration.
The plugin accepts the following options:
- `routes`: The routes to register.
- `middlewares`: Middlewares to apply to all routes.
- `rateLimitOptions`: The rate limit configuration.
- `replyFromOptions`: The reply.from options.
- `middieOptions`: The middie options.
## Routes
Routes can be configured with the following properties:
- `prefix`: The URL prefix for the route.
- `target`: The target backend service URL.
- `bodyLimit`: (Optional) The maximum body size allowed for the route. Default is `1048576` (1MB).
- `method`: (Optional) The HTTP method to match for the route. Default is `*`.
- `pathRegex`: (Optional) A regular expression to match the path. Default is `/*`.
- `prefixRewrite`: (Optional) The URL prefix to rewrite to.
- `middlewares`: (Optional) An array of middlewares to apply to the route.
- `hooks`: (Optional) Additional hooks to pass to the `reply.from` method.
- `config`: (Optional) The Fastify route configuration object.
Rate limit can be configured at the route level or globally. If you want to disable the global rate limit, pass `false` to the `rateLimitOptions.global` property. Check the [docs](https://github.com/fastify/fastify-rate-limit) for all the configurations.
### Example
```javascript
const options = {
rateLimitOptions: {
global: false // Pass false if you want to disable the global rate limit and add configurations to the routes
},
replyFromOptions: {
// reply.from options
},
middieOptions: {
// middie options
},
middlewares: [
// Global middlewares
],
routes: [
{
method: 'GET',
pathRegex: '/*',
prefix: '/api',
target: 'http://backend-service',
config: {
rateLimit: {
max: 10,
timeWindow: '1 minute'
}
}
prefixRewrite: '/backend',
middlewares: [
// Route-level middlewares
],
hooks: {
// Additional hooks
}
}
]
}
app.register(gatewayPlugin, options)
```
## License
This project is licensed under the ISC License - see the [LICENSE](LICENSE) file for details.