Deploy a Fastify App

This guide explains how to deploy a Fastify (opens in a new tab) application to 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 (opens in a new tab). 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 (opens in a new tab)

You can consult the repository on GitHub (opens in a new tab) 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 (opens in a new tab) 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 -a 0.0.0.0 app.js",
...
}

The Fastify application is now ready and can be run locally.

This example application should run on any modern version of Node.js. If your application requires a specific Node.js version, add or set the engines section in your package.json file. Consult the build with Node.js page to learn more.

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}.

Push the project to GitHub

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:

Make sure to replace <YOUR_GITHUB_USERNAME>/<YOUR_REPOSITORY_NAME> with your GitHub username and repository name.

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

Deploy to Koyeb using git-driven deployment

To deploy the Node.js Fastify app on Koyeb, using the control panel (opens in a new tab), follow the steps below:

  1. Click Create Web Service on the Overview tab of the Koyeb control panel.
  2. Select GitHub as the deployment option.
  3. Choose the GitHub repository and branch containing your application code. Alternatively, you can enter our public Fastify example repository (opens in a new tab) into the Public GitHub repository at the bottom of the page: https://github.com/koyeb/example-fastify.
  4. Name your App and Service, for example, fastify-koyeb.
  5. Click the Deploy button.

A Koyeb App and Service will be created. Your application will be built and deployed to 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.

Deploy to 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:

Dockerfile
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 page on deploying pre-built container images.