Fastify
The API server preset using Fastify comes pre-configured to simplify development. Here's what you need to know to get started:
Knowledge of the Fastify framework is essential for optimal use. Read the documentation: https://fastify.dev/
Entry Function
Just like in the project base we have the bootstrapApp function to load modules and start the bot, we also have the bootstrapServer function located in src/server/index.ts
. This function is exported to be called as soon as the bot is ready:
With this setup, the Fastify server will start right after the bot goes online, receiving the Client as an argument.
Environment Variables
The Zod schema for environment validation is adjusted to allow new environment variables:
This means you can change the PORT of the Fastify server by setting this variable in the .env
file.
Automatic Loading and Registration
By default, this preset comes with the @fastify/autoload
plugin installed, allowing us to set up a folder with route handlers that are automatically defined:
This setup will import and register route handlers from all files in the src/server/routes
folder.
Therefore, it is important that all files in the routes folder have a default export of a function that receives the Fastify instance, the Discord client, and the Fastify done function.
Here's an example of a root route returning some information:
The @fastify/autoload
plugin imports subfolder modules and maps the path to a route.
Create a file in src/server/routes/users
:
In the example above, two routes are registered: the first is /users
, and the second is /users/:id
.
Read the full documentation for the plugin to understand the best use cases: https://github.com/fastify/fastify-autoload
Cors
In this preset, the CORS plugin is already installed with a basic configuration:
Read the full documentation for the plugin to understand the best use cases: https://github.com/fastify/fastify-cors