codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
🚂

Express.js

69 / 76 topics
48Setting Up Continuous Integration for Express.js49Using Travis CI with Express.js50Using CircleCI with Express.js68Continuous Deployment for Express.js Applications69Blue-Green Deployments with Express.js70Canary Releases with Express.js
Tutorials/Express.js/Blue-Green Deployments with Express.js
🚂Express.js

Blue-Green Deployments with Express.js

Updated 2026-05-15
10 min read

Blue-Green Deployments with Express.js

Introduction

Blue-green deployment is a popular strategy used in software development to minimize downtime during application updates. This method involves running two identical production environments, often referred to as "blue" and "green". At any given time, one environment (e.g., blue) serves all live traffic while the other (e.g., green) remains idle. When it's time to deploy a new version of the application, the new code is deployed to the inactive environment (green). Once testing confirms that the new version is stable and functioning correctly, traffic is switched from the blue environment to the green one, making the new version live with zero downtime.

This tutorial will guide you through implementing blue-green deployments for Express.js applications using popular CI/CD tools like Jenkins or GitHub Actions. We'll also cover how to manage DNS settings and route traffic between environments effectively.

Concept

The key components of a blue-green deployment are:

  1. Two Production Environments: Blue and Green.
  2. Traffic Switching Mechanism: A way to redirect traffic from one environment to another without downtime.
  3. DNS Management: Ensuring that the domain points to the correct environment.

Steps for Blue-Green Deployment

  1. Deploy New Code: Deploy the new version of your application to the inactive environment (e.g., green).
  2. Test: Verify that the new environment is functioning correctly.
  3. Switch Traffic: Redirect traffic from the active environment (blue) to the new environment (green).
  4. Monitor: Keep an eye on the new environment for any issues.

Examples

Setting Up Two Environments

Assuming you have two servers or containers running your Express.js application, let's set up a basic example using Docker and Nginx as a reverse proxy.

1. Create Dockerfiles for Blue and Green

Create two separate Dockerfiles, one for each environment:

dockerfile
1# Dockerfile-blue
2FROM node:14
3WORKDIR /usr/src/app
4COPY package*.json ./
5RUN npm install
6COPY . .
7EXPOSE 3000
8CMD ["node", "app.js"]
9
10# Dockerfile-green
11FROM node:14
12WORKDIR /usr/src/app
13COPY package*.json ./
14RUN npm install
15COPY . .
16EXPOSE 3001
17CMD ["node", "app.js"]

2. Build and Run Containers

Build and run the containers for both environments:

Terminal
docker build -t express-blue -f Dockerfile-blue .
docker build -t express-green -f Dockerfile-green .
docker run -d --name blue-app -p 3000:3000 express-blue
docker run -d --name green-app -p 3001:3001 express-green

3. Configure Nginx as a Reverse Proxy

Set up an Nginx server to route traffic between the two environments:

nginx
1server {
2 listen 80;
3 server_name yourdomain.com;
4
5 location / {
6 proxy_pass http://blue-app:3000;
7 proxy_set_header Host $host;
8 proxy_set_header X-Real-IP $remote_addr;
9 proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
10 proxy_set_header X-Forwarded-Proto $scheme;
11 }
12}

4. Deploy New Code to Green Environment

Deploy the new version of your application to the green environment:

Terminal
docker build -t express-green-new -f Dockerfile-green .
docker stop green-app
docker rm green-app
docker run -d --name green-app -p 3001:3001 express-green-new

5. Test the Green Environment

Ensure that the new version is functioning correctly:

Terminal
curl http://yourdomain.com
Output
# Expected output from your new application
Hello, World!

6. Switch Traffic to Green Environment

Update the Nginx configuration to route traffic to the green environment:

nginx
1server {
2 listen 80;
3 server_name yourdomain.com;
4
5 location / {
6 proxy_pass http://green-app:3001;
7 proxy_set_header Host $host;
8 proxy_set_header X-Real-IP $remote_addr;
9 proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
10 proxy_set_header X-Forwarded-Proto $scheme;
11 }
12}

7. Monitor the Green Environment

Monitor the green environment for any issues and ensure everything is running smoothly.

What's Next?

After mastering blue-green deployments, you might want to explore canary releases with Express.js. Canary releases allow you to gradually roll out new features or updates to a subset of users before making them available to everyone. This approach helps in identifying potential issues without affecting all users at once.

Check out our next tutorial on "Canary Releases with Express.js" for more advanced deployment strategies.


PreviousContinuous Deployment for Express.js ApplicationsNext Canary Releases with Express.js

Recommended Gear

Continuous Deployment for Express.js ApplicationsCanary Releases with Express.js