Deploy a Fastify application on Koyeb and benefit from native autoscaling, automatic HTTPS (SSL), auto-healing, and global load-balancing across our edge network with zero configuration.
This guide explains how to deploy a Fastify
To successfully follow this documentation, you will need to have a Koyeb account
You can deploy and preview the Fastify application from this guide by clicking the Deploy to Koyeb button below.
You can consult the repository on GitHub
Get started by creating a minimalistic Node.js Fastify application.
You will need Node.js
In your terminal, run the following commands to create the directory that will hold the application code:
mkdir example-fastify cd example-fastify
Koyeb detects Node.js applications when one of the Node.js matching criteria is met.
To generate a new Fastify application, in the example-fastify
directory, run the following command:
$ npm init fastify Need to install the following packages: create-fastify@3.0.0 Ok to proceed? (y) y generated README.md generated .gitignore generated app.js generated plugins/README.md generated routes/README.md generated test/helper.js generated plugins/sensible.js generated plugins/support.js generated routes/root.js generated routes/example/index.js generated test/plugins/support.test.js generated test/routes/example.test.js generated test/routes/root.test.js --> reading package.json in . edited package.json, saving saved package.json --> project fastify-test generated successfully run 'npm install' to install the dependencies run 'npm start' to start the application run 'npm run dev' to start the application with pino-colada pretty logging (not suitable for production) run 'npm test' to execute the unit tests
This command initializes a Fastify project and creates various files and folders:
Before being able to run the application, you need to install the dependencies by running the following command:
npm install
Last, update the package.json
file to modify the start
script and make Fastify listen on all interfaces.
{ ... "scripts": { - "start": "fastify start -l info app.js", + "start": "fastify start -l info -a 0.0.0.0 app.js" ... }
The Fastify application is now ready and can be run locally.
To launch the application locally and make sure everything is running as expected, run the following command in your terminal:
npm run start
Open your browser and visit http://localhost:3000
. Once the application loads, the following content will appear {"root":true}
.
In the project directory, initialize a new git repository by running the following command:
git init
We will use this repository to version the application code and push the changes to a GitHub repository. If you don't have an existing GitHub repository to push the code to, you can create a new one and run the following commands to commit and push changes to your GitHub repository:
git add . git commit -m "Initial commit" git remote add origin git@github.com:<YOUR_GITHUB_USERNAME>/<YOUR_REPOSITORY_NAME>.git git push -u origin main
Make sure to replace <YOUR_GITHUB_USERNAME>/<YOUR_REPOSITORY_NAME>
with your GitHub username and repository name.
To deploy the Node.js Fastify app on Koyeb, using the control panel
fastify-koyeb
A Koyeb App and Service have been created. Your application is now going to be built and deployed on Koyeb. Once the build has finished, you will be able to access your application running on Koyeb by clicking the URL ending with .koyeb.app
.
To deploy the Node.js Fastify app on Koyeb using the Koyeb CLI, run the following command in your terminal:
koyeb app init example-fastify \ --git github.com/<YOUR_GITHUB_USERNAME>/<YOUR_REPOSITORY_NAME> \ --git-branch main \ --ports 8080:http \ --routes /:8080 \ --env PORT=8080
Make sure to replace <YOUR_GITHUB_USERNAME>/<YOUR_REPOSITORY_NAME>
with your GitHub username and repository name.
To track the app deployment and visualize build logs, execute the following command:
koyeb service logs example-fastify/example-fastify -t build
Once the deployment of your application has finished, you can retrieve the public domain to access your application by running the following command:
$ koyeb app get example-fastify ID NAME DOMAINS CREATED AT 55d75993 example-fastify ["example-fastify-myorg.koyeb.app"] 20 Mar 23 09:07 UTC
With your app running, you can track the runtime logs by running the following command:
koyeb service logs example-fastify/example-fastify -t runtime
As an alternative to using git-driven deployment, you can deploy a pre-built container from any public or private registry. This can be useful if your application needs specific system dependencies or you need more control over how the build is performed.
To dockerize the Node.js Fastify application, create a Dockerfile
in your project root directory and copy the content below:
FROM node:lts WORKDIR /app COPY . . RUN npm ci EXPOSE 3000 CMD ["npm", "start"]
The Dockerfile
above provides the minimum requirements to run the Fastify application. You can easily extend it depending on your needs.
To build and push the Docker image to a registry and deploy it on Koyeb, refer to the Deploy an app from a Docker image documentation.