Deploy a Fastify App

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 application on Koyeb using:

  1. Git-driven deployment to automatically build and deploy a new version of your application each time a change is detected on your branch.
  2. Pre-built containers you can deploy from any public or private registry.

To successfully follow this documentation, you will need to have a Koyeb account. You can optionally install the Koyeb CLI if you prefer to follow this guide without leaving the terminal.

You can deploy and preview the Fastify application from this guide by clicking the Deploy to Koyeb button below.

Deploy to Koyeb

You can consult the repository on GitHub to find out more about the example application that this guide uses.

Create the Fastify app

Get started by creating a minimalistic Node.js Fastify application. You will need Node.js installed on your machine.

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:

  • .gitignore: To ignore files that should not be committed to the repository
  • app.js: The main entry point of the application
  • plugins/: To define behavior that is common to all the routes in your application, including authentication, caching, templates, and all the other cross-cutting concerns
  • routes/: To configure all the routes that define the endpoints of your web application
  • test/: To store all the tests for your application
  • README.md: A starter README file for your project
  • package.json: Basic Node.js configuration, metadata, and dependency file

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.

Run the Node.js Fastify app 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}.

Deploy the Node.js Fastify app on Koyeb using git-driven deployment

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.

Via the Koyeb control panel

To deploy the Node.js Fastify app on Koyeb, using the control panel, follow the steps below:

  1. Create a new Koyeb App
  2. Select GitHub as the deployment option
  3. Choose the GitHub repository and branch containing your application code
  4. Name your service, for example, fastify-koyeb
  5. Click the Deploy button.

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.

Via Koyeb CLI

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.

Access deployment logs

To track the app deployment and visualize build logs, execute the following command:

koyeb service logs example-fastify/example-fastify -t build

Access your app

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

Access runtime logs

With your app running, you can track the runtime logs by running the following command:

koyeb service logs example-fastify/example-fastify -t runtime

Deploy the Node.js Fastify app on Koyeb using a pre-built container

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.