To deploy an Axe API project, the process is just as straightforward as deploying any other Node.js application. In this section, we will talk about the deployment an Axe API project.
- You will learn
- What is deployment?
- How to deploy an Axe API project?
- How to execute migrations?
- How to dockerize an Axe API project?
- How to use docker-compose for an Axe API project?
Deployment is the process of making the API available for use on a server or hosting environment. It involves configuration, installing dependencies, and transferring the API code and associated files to the server.
The deployment process may involve steps like environment configuration, code compilation or interpretation, database migration, and setting up security measures.
Once deployed, the API becomes accessible to clients who can send requests and receive responses. Deployment ensures the API is ready for production use, reliable, and scalable to handle user traffic efficiently.
In this section, we will provide all information you need.
Compiling the app
You can compile your application by using the following command;
$ npm run build
After this command, you will have a folder called
build, and the
build folder would have all of your compiled application codes, migration files, and other necessary parts of the application.
Remember that in the
build folder you should define your environment variables in the
.env file. Since this is your production build,
npm run build command does not include the
.env file. You might add
.env file automatically to the build folder by editing the
scripts/postbuild.sh file if you want.
Execute the build
Your compiled codes are basic Node.js codes. So that, in the
build folder, you can execute the application with the following command;
$ node index.js
[18:00:00] INFO: Axe API listens requests on http://localhost:3000
Axe API adds your migrations files to your
build folder. Only thing you should do is executing the migrate command like the following one;
$ knex --esm migrate:latest
Batch 1 run: 2 migrations
Remember that in your machine you must have the
knex cli. You can install it by using the following code:
npm i -g knex
You can find more command example on the Migration CLI Documentation
Axe API adds
Dockerfile to your build folder by default. A standard Axe API Dockerfile looks like the following one;
FROM --platform=linux/amd64 node:18
# Create app directory
# Install app dependencies
COPY package*.json ./
RUN npm install
# Building for a production
RUN npm ci --only=production
# To migrate database changes we need `knex` cli
RUN npm i -g knex
# Bundle app source
COPY . .
CMD ["node", "index.js"]
You can change the
Dockerfile by your requirements.
Once you build your application, you can execute the following command in the
build directory to build a docker image for your application;
$ cd build
$ docker build -t my-api-image .
Once your docker image is ready, you can execute it with the following command;
$ docker run -p 3000:3000 my-api-image
Please keep in your mind that you should provide all environment variables in
.env files or via docker environment parameters.
docker-compose.yml file could be very helpful in your project. You may find a simple example of a
docker-compose.yml file in the following one. You are free to edit the example by your requirements.
command: knex --esm migrate:latest
command: node index.js
You can use
DB_HOST value to access the dabase which on located in your machine. You can find more details in the Docker documentation.
We tried to show some of the basic deployment methods in here. Nevertheless we know that there are many different scenario which you can use. We encrouge Axe API developers to write blog posts abou how to deploy your application in different environments instead of adding many pages here.
No matter in which platform or environment you deploy your application, the following facts would be same;
- You need a supported Node.js version in the machine which you will deploy.
- You need to execute the migration files via
Other than tehese are just implementation details.
In this section, we tried to cover how you can deploy your API.
In the next chapter, you can learn how you can contribute to Axe API.