Writing functional server code is only half the battle; deploying hardened, zero-downtime services to the cloud is what transforms you into a complete engineer. In this tutorial, you will master production deployments with Multi-Stage Docker builds, PM2 Process Manager, and Nginx Reverse Proxying.
Dockerfile# --- STAGE 1: Dependency Installation & Build ---
FROM node:22-alpine AS builder
WORKDIR /app
# Copy dependency manifests first to leverage Docker layer caching
COPY package*.json ./
RUN npm ci
# Copy source code and build
COPY . .
# Prune devDependencies to keep image lean
RUN npm prune --production
# --- STAGE 2: Lightweight Production Runtime ---
FROM node:22-alpine AS runner
WORKDIR /app
ENV NODE_ENV=production
ENV PORT=3000
# Security: Run as unprivileged non-root user bundled in node:alpine
USER node
# Copy only production artifacts from builder stage
COPY --chown=node:node --from=builder /app/package*.json ./
COPY --chown=node:node --from=builder /app/node_modules ./node_modules
COPY --chown=node:node --from=builder /app .
EXPOSE 3000
CMD ["node", "server.js"]
ecosystem.config.js)module.exports = {
apps: [{
name: 'veewom-api',
script: 'server.js',
instances: 'max', // Utilize all available CPU cores automatically!
exec_mode: 'cluster', // Cluster mode
autorestart: true, // Restart if application crashes
watch: false, // Never watch files in production
max_memory_restart: '1G', // Restart if RAM exceeds 1GB
env_production: {
NODE_ENV: 'production',
PORT: 3000
}
}]
};
Essential PM2 Commands:
pm2 start ecosystem.config.js --env production # Start cluster
pm2 reload veewom-api # Zero-downtime rolling reload!
pm2 status # Inspect CPU/RAM of all workers
pm2 logs veewom-api # Stream aggregated logs
server {
listen 80;
server_name api.veewom.com;
location / {
proxy_pass http://127.0.0.1:3000;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
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;
proxy_cache_bypass $http_upgrade;
}
}
Boost your server-side JavaScript proficiency by studying these closely connected core modules:
PM2 automates clustering across all CPU cores.
Run containers as non-root users.
Stream logs in production container environments.