This is a simple and practical example of how to deploy a Node.js API to various environments like production, staging, QA, and development using Docker Compose and NGINX.
-
Clone the repository:
git clone https://github.com/julekgwa/tuts-api.git cd tuts-api
-
Create a
.env
file in the root directory and add the necessary environment variables:PORT=3000 DB_USER=your_mongo_user DB_PASS=your_mongo_password DATABASE=your_database_name DB_HOST=your_db_host NODE_ENV=development DOMAIN=your_domain APP_PORT=3000 # docker container port
-
Build and run the application using Docker Compose:
docker-compose --env-file .env.dev up -d --build --force-recreate
Deploy to other environments:
docker-compose --env-file .env.staging up -d --build --force-recreate docker-compose --env-file .env.qa up -d --build --force-recreate docker-compose --env-file .env.production up -d --build --force-recreate
-
Access the API documentation at
http://${NODE_ENV}.localhost/api/v1/api-docs
. # Replace${NODE_ENV}
with the value of theNODE_ENV
environment variable.
The API documentation is available in the app/swagger/swagger.yml file. It uses OpenAPI 3.0.0 specification.
The NGINX configuration template is available in the nginx/nginx.template.conf file. It is a simple configuration that listens on port 80 and proxies requests to the Node.js application.
worker_processes auto;
events {
worker_connections 1024;
}
http {
upstream app_cluster {
server ${NODE_ENV}_app:${APP_PORT};
}
server {
listen 80;
server_name ${NODE_ENV}.${DOMAIN};
location / {
proxy_pass http://app_cluster;
proxy_set_header Host \$host;
proxy_set_header X-Real-IP \$remote_addr;
proxy_set_header X-Forwarded-For \$proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto \$scheme;
}
}
}
- worker_processes: The number of worker processes that NGINX should use.
- events: The events block defines how NGINX should handle connections.
- http: The http block defines the configuration for the HTTP server.
- upstream: The upstream block defines the backend servers that NGINX should proxy requests to.
- server: The server block defines the configuration for the server.
- listen: The port that NGINX should listen on.
- server_name: The domain name that NGINX should respond to. It uses the
DOMAIN
andNODE_ENV
environment variables. exampledevelopment.localhost
. Note: if you're running this locally, you need to add the domain to your hosts file. For example,127.0.0.1 development.localhost
. then you can access the application athttp://development.localhost
. - location: The location block defines how NGINX should handle requests to a specific URL.
- proxy_pass: The URL that NGINX should proxy requests to.
- proxy_set_header: Sets the headers that NGINX should pass to the backend server.
Add the following entries to your hosts file:
sudo nano /etc/hosts
Add the following entries:
127.0.0.1 development.localhost
127.0.0.1 staging.localhost
127.0.0.1 qa.localhost
127.0.0.1 production.localhost
- Express: Web framework for Node.js.
- Swagger: API documentation and validation.
- Docker: Containerization.
- NGINX: Reverse proxy server.
This project is licensed under the MIT License.