How to build a Fastify environment with core modules, CLI and Docker

I will show you how to build a Fastify environment. The code will also be posted on Git Hub, so please refer to it.

overview

Explains various environment construction methods of Fastify, a Node.js-based web framework.

In this article, we will introduce how to build the environment in the following three ways.


  1. How to build an environment with Fastify core modules
  2. How to build an environment using Fastify CLI
  3. How to build an environment using Docker

Here, we will explain how to build a Fastify environment using the following versions.

Fastify v4.0.0
nodejs v19.7.0

Also, all the code I’ve created so far is listed below. https://github.com/wiblok/Fastify

*** Fastify works with Node.js. If Node.js is not installed, please download it from the official website and install it. ** node.js

How to build an environment using core modules

This method requires and uses Fastify’s core modules. Please follow the steps below.

To build an environment with Fastify alone, the following prerequisites are required

Create project directory

First, create a new directory to create your Fastify project. Run the below command in command line

mkdir fastify-core-project

Change to project directory

Go to your project directory. Please run the following command:

cd fastify-core-project

After moving, move to the root directory of the project.

Project initialization

Then initialize the project in the created directory. Create a package.json file by running the following command

npm init -y

This command will generate package.json with default settings.

Installing Fastify

Install the Fastify package. Please run the below command

npm install fastify@4.0.0

This will add Fastify to your project.

Creating an application

Create a Fastify application. Create a new file (e.g. app.js) inside your project directory and add the following code

const fastify = require('fastify');

const app = fastify();

app.get('/', (request, reply) => {
  reply.send('Hello, Fastify!');
});

app.listen({ port: 3000 }, (err) => {
  if (err) {
    console.error('An error occurred while starting the server:', err);
    process.exit(1);
  }
  console.log('Server started on port 3000');
});

The example above shows the basic structure of a Fastify application. Define a handler for the root path using the app.get() method and return Hello, Fastify! as a response.

The final directory structure will look like this:

fastify-core-project
├── node_modules
│ └── (dependency package)
├── app.js
├── package-lock.json
└── package.json

Start the server

Finally, start the Fastify server. Please run the below command

node app.js

If the server starts successfully, you should see “Server started on port 3000” in the console. Visit http://localhost:3000 in your browser to make sure your Fastify application is working properly.

How to build environment using CLI

Fastify can also build environments using the Fastify CLI. Fastify CLI is a command line tool for easily creating Fastify projects. This article details the steps to build an environment using the Fastify CLI.

Install Fastify CLI

Install the Fastify CLI globally. Please run the following command:

npm install -g fastify-cli

This will install the Fastify CLI on your system.

Creating a project

Run the following command to create a Fastify project:

fastify generate fastify-cli-project

The above command will create a project directory named fastify-cli-project. You can change the directory name to anything you like.

Change to project directory

Go to your project directory. Please run the following command:

cd fastify-cli-project

After moving, move to the root directory of the project.

Installing dependencies

In your project root directory, install the dependencies: Please run the following command:

npm install

This will add Fastify and related packages to your project. The final directory structure will look like this:

fastify-cli-project
├── node_modules
│ └── (dependency package)
├── plugins
│ ├── README.md
| ├── sensible.js
| └── support.js
├── routes
| ├── example
| | └── index.js
| ├── README.md
│ └── root.js
├── test
│ ├── plugins
| | └── support.test.js
| ├── routes
| | ├── example.test.js
| | └── root.test.js
| └── helpers.js
├── .gitignore
├── app.js
├── package.json
├── package-lock.json
└── README.md

Start the server

Start the Fastify server. Please run the following command:

npm start

If the server starts successfully, you should see “Server started on port 3000” in the console. Visit http://localhost:3000 in your browser to make sure your Fastify application is working properly.

Fastify CLI makes setting up your Fastify project easy. Just install the Fastify CLI, create a project using the fastify generate command, install dependencies, and you can quickly start developing Fastify applications.

How to build an environment using Docker

First of all, install Docker. Download the Docker installer for your operating system from the official Docker website and install it.

Create project directory

First, create a new directory to create your Fastify project. Run the below command in command line

mkdir fastify-docker-project

Change to project directory

Go to your project directory. Please run the following command:

cd fastify-docker-project

After moving, move to the root directory of the project.

Project initialization

Run the following command to initialize the Fastify project.

npm init -y

Install Required Packages

Install Fastify and the packages needed to create Docker images.

npm install fastify@4.0.0

Creating a Dockerfile

To build your Fastify application as a Docker image, create a file named Dockerfile in your project folder and add the following content.

FROM node:19.7.0-alpine

WORKDIR /app

COPY package*.json ./

COPY..

EXPOSE 3000

CMD ["npm", "start"]

Creating an application

Create a Fastify application. Create a new file (e.g. app.js) inside your project directory and add the following code

Dcoker listens on 0.0.0.0 to allow connections from all hosts.


const fastify = require('fastify');

const app = fastify();

app.get('/', (request, reply) => {
  reply.send('Hello, Fastify!');
});

app.listen({ port: 3000, host: "0.0.0.0" }, (err, address) => {
  if (err) {
    console.error('An error occurred while starting the server:', err);
    process.exit(1);
  }
  console.log('Server started on port 3000');
});

Building a Docker image

Build the Docker image using the command below.

docker build -t fastify-docker-project .

Running a Docker container

Run a Docker container from the built Docker image.

docker run -p 3000:3000 -d fastify-docker-project

This will run your Fastify application inside a Docker container and map it to port 3000 on your host. You can access your Fastify application by visiting http://localhost:3000 from your browser, API test tool, etc.

The above is the procedure for building a Fastify application environment using Docker. By using Docker, you can reduce the work of building the environment and make the development process smoother.

Note: Access to port 3000 may be restricted by the host’s firewall or network settings. Adjust your firewall and network settings as needed.