Deploying a Node App to Fly.io
This recipe guides you through deploying your Nx based Node backend application to Fly.io.
If you don't have an Nx powered Node project already, you can create a new one
npx create-nx-workspace@latest my-api \
--preset=node-standalone \
--framework=fastify \
--docker
This example uses Fastify but you can equally use Express, Koa or NestJS by selecting those frameworks during project creation.
You can also install the @nx/node
package into an existing Nx monorepo and generate a new Node application.
Starting with Nx 15.7 we now have first-class support for building Node backend applications
In this video, we're going to explore how to modularize a Node backend application
Setup Docker
If you don't have a Docker setup already, you can leverage the setup-docker
generator from the @nx/node
package to create one:
npx nx g @nx/node:setup-docker
Deploying the Server to Fly.io
Now, all we need to do is set up Fly.io and deploy! If you haven't used Fly.io before, you need to install the CLI and create an account. It'll only take a couple of minutes.
- Install flyctl - This is the Fly.io CLI.
- Create an account with
fly auth signup
orfly auth login
.
If you run into any issues, please refer to their getting started guide.
Once you have authenticated using fly
, we are ready to launch our project.
fly launch --generate-name --no-deploy
The setup process of Fly asks about creating a .dockerignore
file based on the current .gitignore
. If you confirm that step, make sure to remove **/dist
from the .dockerignore
file, otherwise the build will fail because the Dockerfile
copies the build output from the dist/
folder.
Once the setup completes, update the fly.toml
file and make sure it uses the correct port:
[[services]]
http_checks = []
internal_port = 3000 # Make sure this matches the port in Dockerfile
Now we can build and deploy the server.
nx build
fly deploy
Fly.io will log out the monitoring link when the server is successfully deployed. You can open the server in a browser using the fly open
command.
That's is! Our server is now deployed for the world to use.
Optional: Adding a Deploy Target
You can also automate the deployment by adding a target to your project. In addition, that allows us to leverage the Nx task pipeline to make sure we first run the build
and then the deploy
.
By using Nx run-commands, you can add a deploy
target to the project. Go to the project's project.json
file (under "targets"
) and add the following:
"deploy": {
"dependsOn": [
"build"
],
"command": "fly deploy"
}
Then you can run nx deploy
, which will run the build (if necessary) before deploying. If the build ran before, it would be cached.