To deploy an Express.js app to production on an Ubuntu server, run it under a process manager such as PM2 or in Docker, put Nginx in front as a reverse proxy, add a Let’s Encrypt certificate, and keep secrets in environment variables. This guide gives the code for each piece, from the app structure to the Nginx config. The last section shows the shorter route: connect your server to CloudPloy and let an AI agent deploy the repo.
Node.js 24 and 26 are the current LTS lines, and Node 22 is supported until April 2027. Node 20 reached end of life on April 30, 2026, so start new servers on 24 or newer. See the Node.js release table. npm install express now installs Express 5, so if your code is older, read the Express 5 migration guide first.
What does a production Express deployment need?
- A process manager. PM2 or Docker restarts the app after a crash and after a reboot.
- A reverse proxy. Nginx terminates HTTPS, serves static files and passes requests to Node.
- Secrets outside the repo. Keep the database URL, JWT secret and API keys in environment variables.
- Security middleware and rate limits. Express’s own security best practices list the baseline, including Helmet and TLS.
- Health checks and logs. You need to know when the app is down and why.
Express’s performance best practices cover the rest, such as gzip, caching and running in production mode.
Production-Ready Express.js Application Structure
Enterprise-Grade Express Application
// app.js - Production Express application
import express from 'express';
import helmet from 'helmet';
import compression from 'compression';
import rateLimit from 'express-rate-limit';
import mongoSanitize from 'express-mongo-sanitize';
import xss from 'xss-clean';
import hpp from 'hpp';
import cors from 'cors';
import morgan from 'morgan';
import responseTime from 'response-time';
import slowDown from 'express-slow-down';
import { createServer } from 'http';
import gracefulShutdown from 'http-graceful-shutdown';
import cluster from 'cluster';
import os from 'os';
// Import routes and middleware
import authRoutes from './routes/auth.js';
import userRoutes from './routes/users.js';
import apiRoutes from './routes/api.js';
import { errorHandler } from './middleware/error.js';
import { notFound } from './middleware/notFound.js';
import { authenticate } from './middleware/auth.js';
import config from './config/index.js';
const app = express();
// Trust proxy for accurate client IPs
app.set('trust proxy', 1);
// Security middleware
app.use(helmet({
contentSecurityPolicy: {
directives: {
defaultSrc: ["'self'"],
scriptSrc: ["'self'", "'unsafe-inline'"],
styleSrc: ["'self'", "'unsafe-inline'"],
imgSrc: ["'self'", "data:", "https:"],
},
},
hsts: {
maxAge: 31536000,
includeSubDomains: true,
preload: true
}
}));
// CORS configuration
app.use(cors({
origin: (origin, callback) => {
const allowedOrigins = config.cors.origins;
if (!origin || allowedOrigins.includes(origin)) {
callback(null, true);
} else {
callback(new Error('Not allowed by CORS'));
}
},
credentials: true,
methods: ['GET', 'POST', 'PUT', 'DELETE', 'OPTIONS'],
allowedHeaders: ['Content-Type', 'Authorization', 'X-Requested-With']
}));
// Rate limiting with different tiers
const createRateLimit = (windowMs, max, message) => rateLimit({
windowMs,
max,
message: { error: message },
standardHeaders: true,
legacyHeaders: false,
handler: (req, res) => {
res.status(429).json({
error: message,
retryAfter: Math.round(windowMs / 1000)
});
}
});
// Global rate limiting
app.use('/api/', createRateLimit(
15 * 60 * 1000, // 15 minutes
100, // 100 requests per window
'Too many requests from this IP, please try again later'
));
// Stricter limits for auth endpoints
app.use('/api/auth/', createRateLimit(
15 * 60 * 1000, // 15 minutes
10, // 10 requests per window
'Too many authentication attempts, please try again later'
));
// Progressive delay for repeated requests
const speedLimiter = slowDown({
windowMs: 15 * 60 * 1000, // 15 minutes
delayAfter: 50, // allow 50 requests per windowMs without delay
delayMs: 500, // add 500ms delay per request after delayAfter
maxDelayMs: 20000, // maximum delay of 20 seconds
});
app.use('/api/', speedLimiter);
// Data sanitization
app.use(mongoSanitize()); // NoSQL injection protection
app.use(xss()); // XSS protection
app.use(hpp()); // HTTP Parameter Pollution protection
// Performance middleware
app.use(compression({
level: 6,
threshold: 1000,
filter: (req, res) => {
if (req.headers['x-no-compression']) {
return false;
}
return compression.filter(req, res);
}
}));
app.use(responseTime((req, res, time) => {
console.log(`${req.method} ${req.url} - ${time}ms`);
}));
// Body parsing with size limits
app.use(express.json({
limit: '10mb',
verify: (req, res, buf) => {
req.rawBody = buf;
}
}));
app.use(express.urlencoded({
extended: true,
limit: '10mb',
parameterLimit: 1000
}));
// Request logging
if (config.env === 'production') {
app.use(morgan('combined', {
stream: {
write: (message) => {
console.log(message.trim());
}
}
}));
} else {
app.use(morgan('dev'));
}
// Health check endpoint
app.get('/health', (req, res) => {
const healthCheck = {
status: 'healthy',
timestamp: Date.now(),
uptime: process.uptime(),
environment: config.env,
version: process.env.npm_package_version || '1.0.0',
memory: {
used: Math.round(process.memoryUsage().heapUsed / 1024 / 1024),
total: Math.round(process.memoryUsage().heapTotal / 1024 / 1024),
limit: Math.round(process.memoryUsage().rss / 1024 / 1024)
},
pid: process.pid
};
res.status(200).json(healthCheck);
});
// API versioning
app.use('/api/v1/auth', authRoutes);
app.use('/api/v1/users', authenticate, userRoutes);
app.use('/api/v1', authenticate, apiRoutes);
// Error handling middleware
app.use(notFound);
app.use(errorHandler);
// Graceful shutdown handling
process.on('SIGTERM', () => {
console.log('SIGTERM received, shutting down gracefully');
server.close(() => {
console.log('Process terminated');
process.exit(0);
});
});
process.on('SIGINT', () => {
console.log('SIGINT received, shutting down gracefully');
server.close(() => {
console.log('Process terminated');
process.exit(0);
});
});
// Uncaught exception handler
process.on('uncaughtException', (error) => {
console.error('Uncaught Exception:', error);
process.exit(1);
});
process.on('unhandledRejection', (reason, promise) => {
console.error('Unhandled Rejection at:', promise, 'reason:', reason);
process.exit(1);
});
const server = createServer(app);
const PORT = config.port || 3000;
server.listen(PORT, () => {
console.log(`Express server running on port ${PORT} in ${config.env} mode`);
console.log(`Process ID: ${process.pid}`);
console.log(`Memory usage: ${Math.round(process.memoryUsage().rss / 1024 / 1024)}MB`);
});
export { app, server };
Advanced Middleware Configuration
// middleware/error.js - Comprehensive error handling
import { StatusCodes } from 'http-status-codes';
class AppError extends Error {
constructor(message, statusCode, isOperational = true) {
super(message);
this.statusCode = statusCode;
this.status = `${statusCode}`.startsWith('4') ? 'fail' : 'error';
this.isOperational = isOperational;
Error.captureStackTrace(this, this.constructor);
}
}
const handleCastErrorDB = (err) => {
const message = `Invalid ${err.path}: ${err.value}`;
return new AppError(message, StatusCodes.BAD_REQUEST);
};
const handleDuplicateFieldsDB = (err) => {
const value = err.errmsg.match(/(["'])(\\?.)*?\1/)[0];
const message = `Duplicate field value: ${value}. Please use another value!`;
return new AppError(message, StatusCodes.BAD_REQUEST);
};
const handleValidationErrorDB = (err) => {
const errors = Object.values(err.errors).map(el => el.message);
const message = `Invalid input data. ${errors.join('. ')}`;
return new AppError(message, StatusCodes.BAD_REQUEST);
};
const handleJWTError = () =>
new AppError('Invalid token. Please log in again!', StatusCodes.UNAUTHORIZED);
const handleJWTExpiredError = () =>
new AppError('Your token has expired! Please log in again.', StatusCodes.UNAUTHORIZED);
const sendErrorDev = (err, req, res) => {
// API error
if (req.originalUrl.startsWith('/api')) {
return res.status(err.statusCode).json({
status: err.status,
error: err,
message: err.message,
stack: err.stack
});
}
// Rendered website error
console.error('ERROR:', err);
return res.status(err.statusCode).render('error', {
title: 'Something went wrong!',
msg: err.message
});
};
const sendErrorProd = (err, req, res) => {
// API error
if (req.originalUrl.startsWith('/api')) {
// Operational, trusted error: send message to client
if (err.isOperational) {
return res.status(err.statusCode).json({
status: err.status,
message: err.message
});
}
// Programming or other unknown error: don't leak error details
console.error('ERROR:', err);
return res.status(StatusCodes.INTERNAL_SERVER_ERROR).json({
status: 'error',
message: 'Something went wrong!'
});
}
// Rendered website error
if (err.isOperational) {
console.log(err);
return res.status(err.statusCode).render('error', {
title: 'Something went wrong!',
msg: err.message
});
}
console.error('ERROR:', err);
return res.status(err.statusCode).render('error', {
title: 'Something went wrong!',
msg: 'Please try again later.'
});
};
export const errorHandler = (err, req, res, next) => {
err.statusCode = err.statusCode || StatusCodes.INTERNAL_SERVER_ERROR;
err.status = err.status || 'error';
if (process.env.NODE_ENV === 'development') {
sendErrorDev(err, req, res);
} else {
let error = { ...err };
error.message = err.message;
if (error.name === 'CastError') error = handleCastErrorDB(error);
if (error.code === 11000) error = handleDuplicateFieldsDB(error);
if (error.name === 'ValidationError') error = handleValidationErrorDB(error);
if (error.name === 'JsonWebTokenError') error = handleJWTError();
if (error.name === 'TokenExpiredError') error = handleJWTExpiredError();
sendErrorProd(error, req, res);
}
};
export { AppError };
Authentication and Authorization Middleware
// middleware/auth.js - JWT authentication and authorization
import jwt from 'jsonwebtoken';
import { promisify } from 'util';
import User from '../models/User.js';
import { AppError } from './error.js';
import { StatusCodes } from 'http-status-codes';
import config from '../config/index.js';
const signToken = (id) => {
return jwt.sign({ id }, config.jwt.secret, {
expiresIn: config.jwt.expiresIn,
issuer: 'expressapp',
audience: 'expressapp-users'
});
};
const createSendToken = (user, statusCode, req, res) => {
const token = signToken(user._id);
res.cookie('jwt', token, {
expires: new Date(
Date.now() + config.jwt.cookieExpiresIn * 24 * 60 * 60 * 1000
),
httpOnly: true,
secure: req.secure || req.headers['x-forwarded-proto'] === 'https',
sameSite: 'strict'
});
// Remove password from output
user.password = undefined;
res.status(statusCode).json({
status: 'success',
token,
data: {
user
}
});
};
export const authenticate = async (req, res, next) => {
try {
// 1) Getting token and check if it exists
let token;
if (
req.headers.authorization &&
req.headers.authorization.startsWith('Bearer')
) {
token = req.headers.authorization.split(' ')[1];
} else if (req.cookies.jwt) {
token = req.cookies.jwt;
}
if (!token) {
return next(
new AppError('You are not logged in! Please log in to get access.', StatusCodes.UNAUTHORIZED)
);
}
// 2) Verification token
const decoded = await promisify(jwt.verify)(token, config.jwt.secret);
// 3) Check if user still exists
const currentUser = await User.findById(decoded.id);
if (!currentUser) {
return next(
new AppError('The user belonging to this token does no longer exist.', StatusCodes.UNAUTHORIZED)
);
}
// 4) Check if user changed password after the token was issued
if (currentUser.changedPasswordAfter(decoded.iat)) {
return next(
new AppError('User recently changed password! Please log in again.', StatusCodes.UNAUTHORIZED)
);
}
// 5) Check if user account is active
if (!currentUser.active) {
return next(
new AppError('Your account has been deactivated. Please contact support.', StatusCodes.FORBIDDEN)
);
}
// Grant access to protected route
req.user = currentUser;
res.locals.user = currentUser;
next();
} catch (error) {
return next(new AppError('Invalid token. Please log in again!', StatusCodes.UNAUTHORIZED));
}
};
export const authorize = (...roles) => {
return (req, res, next) => {
if (!roles.includes(req.user.role)) {
return next(
new AppError('You do not have permission to perform this action', StatusCodes.FORBIDDEN)
);
}
next();
};
};
export const rateLimitByUser = (maxRequests = 100, windowMs = 15 * 60 * 1000) => {
const store = new Map();
return (req, res, next) => {
if (!req.user) {
return next();
}
const userId = req.user._id.toString();
const now = Date.now();
const windowStart = now - windowMs;
if (!store.has(userId)) {
store.set(userId, []);
}
const userRequests = store.get(userId);
// Remove old requests
const validRequests = userRequests.filter(time => time > windowStart);
if (validRequests.length >= maxRequests) {
return res.status(StatusCodes.TOO_MANY_REQUESTS).json({
error: 'Too many requests for this user',
retryAfter: Math.round(windowMs / 1000)
});
}
validRequests.push(now);
store.set(userId, validRequests);
// Cleanup old entries periodically
if (Math.random() < 0.01) { // 1% chance
for (const [key, requests] of store.entries()) {
const validReqs = requests.filter(time => time > windowStart);
if (validReqs.length === 0) {
store.delete(key);
} else {
store.set(key, validReqs);
}
}
}
next();
};
};
export { createSendToken };
Production Process Management with PM2
Advanced PM2 Configuration
// ecosystem.config.js - Production PM2 configuration
module.exports = {
apps: [{
name: 'express-api',
script: './dist/app.js',
instances: 'max',
exec_mode: 'cluster',
// Environment configuration
env: {
NODE_ENV: 'development',
PORT: 3000,
NODE_OPTIONS: '--enable-source-maps'
},
env_production: {
NODE_ENV: 'production',
PORT: 3000,
NODE_OPTIONS: '--enable-source-maps --max-old-space-size=2048'
},
env_staging: {
NODE_ENV: 'staging',
PORT: 3000,
NODE_OPTIONS: '--enable-source-maps'
},
// Advanced process management
max_memory_restart: '1G',
min_uptime: '10s',
max_restarts: 10,
autorestart: true,
watch: false,
ignore_watch: ['node_modules', 'logs', '.git'],
// Graceful shutdown
kill_timeout: 5000,
wait_ready: true,
listen_timeout: 10000,
// Logging configuration
error_file: './logs/pm2/error.log',
out_file: './logs/pm2/out.log',
log_file: './logs/pm2/combined.log',
merge_logs: true,
time: true,
log_date_format: 'YYYY-MM-DD HH:mm:ss Z',
// Instance management
instance_var: 'INSTANCE_ID',
// Health monitoring
health_check_grace_period: 3000,
health_check_interval: 30000,
health_check_path: '/health',
// Performance monitoring
pmx: true,
automation: false,
// Source map support
source_map_support: true,
// Deployment hooks
post_update: ['npm install', 'npm run build'],
// Clustering options
increment_var: 'PORT',
// Memory and CPU limits
max_memory_restart: '1024M',
// Custom environment variables
env_file: '.env',
// Startup delay
startup_delay: 1000,
// Process title
name: 'express-api-worker'
}],
deploy: {
production: {
user: 'deploy',
host: ['prod-server1.com', 'prod-server2.com'],
ref: 'origin/main',
repo: 'git@github.com:company/express-api.git',
path: '/var/www/express-api',
'pre-deploy-local': 'npm test && npm run lint',
'post-deploy': 'npm install && npm run build && pm2 reload ecosystem.config.js --env production && pm2 save',
'pre-setup': 'mkdir -p /var/www/express-api && mkdir -p /var/www/express-api/logs/pm2'
},
staging: {
user: 'deploy',
host: 'staging-server.com',
ref: 'origin/develop',
repo: 'git@github.com:company/express-api.git',
path: '/var/www/express-api-staging',
'post-deploy': 'npm install && npm run build && pm2 reload ecosystem.config.js --env staging'
}
}
};
PM2 Production Commands
# Production deployment commands
# Install PM2 globally
npm install -g pm2
# Start application in production
pm2 start ecosystem.config.js --env production
# Zero-downtime reload
pm2 reload express-api
# Scale to specific number of instances
pm2 scale express-api 8
# Monitor all processes
pm2 monit
# View logs in real-time
pm2 logs express-api --lines 100 --timestamp
# View process status
pm2 status
# Restart specific process
pm2 restart express-api
# Stop all processes
pm2 stop all
# Delete all processes
pm2 delete all
# Save current process list
pm2 save
# Resurrect saved processes
pm2 resurrect
# Startup script for auto-restart on boot
pm2 startup systemd
pm2 save
# Memory usage and statistics
pm2 info express-api
# Reset restart counter
pm2 reset express-api
# Reload with environment
pm2 reload ecosystem.config.js --env production --update-env
# Deploy to production
pm2 deploy ecosystem.config.js production
# Show process table
pm2 ps
# Plus monitoring (requires keymetrics account)
pm2 plus
# Update PM2
pm2 update
Docker Production Setup
Multi-Stage Production Dockerfile
# syntax=docker/dockerfile:1
FROM node:24-alpine AS base
# Install security updates
RUN apk update && apk upgrade && apk add --no-cache dumb-init
# Create app directory and user
RUN addgroup -g 1001 -S nodejs && \
adduser -S expressjs -u 1001 -G nodejs
# Set working directory
WORKDIR /app
# Copy package files
COPY package*.json ./
COPY yarn.lock ./
# Development stage
FROM base AS development
RUN apk add --no-cache python3 make g++
RUN yarn install --frozen-lockfile
COPY . .
USER expressjs
EXPOSE 3000 9229
CMD ["yarn", "dev"]
# Build stage
FROM base AS builder
RUN apk add --no-cache python3 make g++
# Install dependencies
COPY package*.json ./
COPY yarn.lock ./
RUN yarn install --frozen-lockfile --production=false
# Copy source and build
COPY . .
RUN yarn build && yarn test
# Production dependencies
RUN yarn install --production --frozen-lockfile && yarn cache clean
# Production stage
FROM node:24-alpine AS production
# Install dumb-init and security updates
RUN apk update && apk upgrade && apk add --no-cache dumb-init curl
# Create non-root user
RUN addgroup -g 1001 -S nodejs && \
adduser -S expressjs -u 1001 -G nodejs
# Set working directory
WORKDIR /app
# Copy built application from builder stage
COPY --from=builder --chown=expressjs:nodejs /app/dist ./dist
COPY --from=builder --chown=expressjs:nodejs /app/node_modules ./node_modules
COPY --from=builder --chown=expressjs:nodejs /app/package.json ./
COPY --from=builder --chown=expressjs:nodejs /app/ecosystem.config.js ./
# Create logs directory
RUN mkdir -p logs/pm2 && chown -R expressjs:nodejs logs
# Switch to non-root user
USER expressjs
# Expose port
EXPOSE 3000
# Add health check
HEALTHCHECK --interval=30s --timeout=10s --start-period=40s --retries=3 \
CMD curl -f http://localhost:3000/health || exit 1
# Set production environment
ENV NODE_ENV=production \
NPM_CONFIG_CACHE=/tmp/.npm \
NPM_CONFIG_LOGLEVEL=warn
# Use dumb-init to handle signals properly
ENTRYPOINT ["dumb-init", "--"]
# Start with PM2
CMD ["./node_modules/.bin/pm2-runtime", "start", "ecosystem.config.js", "--env", "production"]
Docker Compose for Full Stack
version: '3.8'
services:
app:
build:
context: .
target: production
dockerfile: Dockerfile
restart: unless-stopped
ports:
- "3000:3000"
environment:
- NODE_ENV=production
- DATABASE_URL=mongodb://mongo:27017/expressapp
- REDIS_URL=redis://redis:6379
- JWT_SECRET=${JWT_SECRET}
- SESSION_SECRET=${SESSION_SECRET}
volumes:
- app_logs:/app/logs
depends_on:
- mongo
- redis
networks:
- app-network
deploy:
resources:
limits:
memory: 1G
cpus: '0.5'
reservations:
memory: 512M
cpus: '0.25'
healthcheck:
test: ["CMD", "curl", "-f", "http://localhost:3000/health"]
interval: 30s
timeout: 10s
retries: 3
start_period: 40s
mongo:
image: mongo:6-jammy
restart: unless-stopped
environment:
MONGO_INITDB_ROOT_USERNAME: ${MONGO_ROOT_USER}
MONGO_INITDB_ROOT_PASSWORD: ${MONGO_ROOT_PASS}
MONGO_INITDB_DATABASE: expressapp
volumes:
- mongo_data:/data/db
- ./mongo-init.js:/docker-entrypoint-initdb.d/mongo-init.js:ro
ports:
- "27017:27017"
networks:
- app-network
deploy:
resources:
limits:
memory: 512M
cpus: '0.5'
command: mongod --auth --bind_ip_all
redis:
image: redis:7-alpine
restart: unless-stopped
command: >
redis-server
--requirepass ${REDIS_PASSWORD}
--appendonly yes
--maxmemory 256mb
--maxmemory-policy allkeys-lru
volumes:
- redis_data:/data
ports:
- "6379:6379"
networks:
- app-network
deploy:
resources:
limits:
memory: 256M
cpus: '0.25'
healthcheck:
test: ["CMD", "redis-cli", "--raw", "incr", "ping"]
interval: 30s
timeout: 10s
retries: 3
nginx:
image: nginx:alpine
restart: unless-stopped
ports:
- "80:80"
- "443:443"
volumes:
- ./nginx.conf:/etc/nginx/nginx.conf:ro
- ./ssl:/etc/nginx/ssl:ro
- nginx_logs:/var/log/nginx
depends_on:
- app
networks:
- app-network
deploy:
resources:
limits:
memory: 128M
cpus: '0.25'
volumes:
mongo_data:
driver: local
redis_data:
driver: local
app_logs:
driver: local
nginx_logs:
driver: local
networks:
app-network:
driver: bridge
ipam:
config:
- subnet: 172.20.0.0/16
Database Integration Patterns
The examples use MongoDB. To host the database itself, see the MongoDB hosting guide.
MongoDB with Mongoose (Advanced)
// models/BaseModel.js - Abstract base model
import mongoose from 'mongoose';
import mongoosePaginate from 'mongoose-paginate-v2';
import { createHash } from 'crypto';
const baseOptions = {
timestamps: true,
toJSON: {
virtuals: true,
transform: function(doc, ret) {
delete ret._id;
delete ret.__v;
return ret;
}
},
toObject: { virtuals: true }
};
export const createBaseSchema = (definition, options = {}) => {
const schema = new mongoose.Schema(definition, {
...baseOptions,
...options
});
// Add pagination plugin
schema.plugin(mongoosePaginate);
// Add soft delete functionality
schema.add({
deletedAt: {
type: Date,
default: null
}
});
// Soft delete methods
schema.methods.softDelete = function() {
this.deletedAt = new Date();
return this.save();
};
schema.methods.restore = function() {
this.deletedAt = null;
return this.save();
};
// Query helpers to exclude deleted documents
schema.query.notDeleted = function() {
return this.where({ deletedAt: null });
};
// Add auditing
schema.add({
createdBy: {
type: mongoose.Schema.Types.ObjectId,
ref: 'User'
},
updatedBy: {
type: mongoose.Schema.Types.ObjectId,
ref: 'User'
}
});
// Version control
schema.add({
version: {
type: Number,
default: 1
}
});
// Pre-save middleware for versioning
schema.pre('save', function(next) {
if (this.isModified() && !this.isNew) {
this.version += 1;
}
next();
});
// Add search text index
schema.methods.updateSearchText = function() {
const searchableFields = this.schema.paths;
const searchTexts = [];
for (const path in searchableFields) {
if (searchableFields[path].instance === 'String' && this[path]) {
searchTexts.push(this[path]);
}
}
this.searchText = searchTexts.join(' ').toLowerCase();
};
schema.add({
searchText: {
type: String,
index: true
}
});
return schema;
};
// Advanced User model
const userSchema = createBaseSchema({
email: {
type: String,
required: [true, 'Email is required'],
unique: true,
lowercase: true,
index: true,
validate: {
validator: function(email) {
return /^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(email);
},
message: 'Please provide a valid email'
}
},
password: {
type: String,
required: [true, 'Password is required'],
minlength: 8,
select: false,
validate: {
validator: function(password) {
return /^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[@$!%*?&])[A-Za-z\d@$!%*?&]/.test(password);
},
message: 'Password must contain at least one uppercase letter, one lowercase letter, one number, and one special character'
}
},
profile: {
firstName: {
type: String,
required: [true, 'First name is required'],
trim: true,
maxlength: [50, 'First name cannot exceed 50 characters']
},
lastName: {
type: String,
required: [true, 'Last name is required'],
trim: true,
maxlength: [50, 'Last name cannot exceed 50 characters']
},
avatar: {
type: String,
default: function() {
const hash = createHash('md5').update(this.email).digest('hex');
return `https://www.gravatar.com/avatar/${hash}?d=identicon`;
}
},
bio: {
type: String,
maxlength: [500, 'Bio cannot exceed 500 characters']
},
location: String,
website: {
type: String,
validate: {
validator: function(url) {
return !url || /^https?:\/\/.+/.test(url);
},
message: 'Website must be a valid URL'
}
}
},
role: {
type: String,
enum: {
values: ['user', 'admin', 'moderator'],
message: 'Role must be user, admin, or moderator'
},
default: 'user'
},
permissions: [{
type: String,
enum: ['read', 'write', 'delete', 'admin']
}],
status: {
type: String,
enum: ['active', 'inactive', 'suspended', 'pending'],
default: 'pending'
},
emailVerified: {
type: Boolean,
default: false
},
emailVerificationToken: String,
passwordResetToken: String,
passwordResetExpires: Date,
loginAttempts: {
type: Number,
default: 0
},
lockUntil: Date,
lastLogin: Date,
lastActive: Date,
preferences: {
notifications: {
email: { type: Boolean, default: true },
push: { type: Boolean, default: true },
marketing: { type: Boolean, default: false }
},
privacy: {
profileVisibility: {
type: String,
enum: ['public', 'private', 'friends'],
default: 'public'
},
showEmail: { type: Boolean, default: false }
},
theme: {
type: String,
enum: ['light', 'dark', 'auto'],
default: 'auto'
},
language: {
type: String,
default: 'en'
}
}
});
// Compound indexes for performance
userSchema.index({ email: 1, status: 1 });
userSchema.index({ 'profile.firstName': 'text', 'profile.lastName': 'text' });
userSchema.index({ role: 1, status: 1 });
userSchema.index({ createdAt: -1 });
userSchema.index({ lastActive: -1 });
// Virtual properties
userSchema.virtual('profile.fullName').get(function() {
return `${this.profile.firstName} ${this.profile.lastName}`;
});
userSchema.virtual('isLocked').get(function() {
return !!(this.lockUntil && this.lockUntil > Date.now());
});
userSchema.virtual('isVerified').get(function() {
return this.emailVerified && this.status === 'active';
});
// Instance methods
userSchema.methods.comparePassword = async function(candidatePassword) {
return bcrypt.compare(candidatePassword, this.password);
};
userSchema.methods.generateAuthToken = function() {
return jwt.sign(
{
id: this._id,
email: this.email,
role: this.role,
permissions: this.permissions
},
process.env.JWT_SECRET,
{ expiresIn: process.env.JWT_EXPIRES_IN }
);
};
userSchema.methods.hasPermission = function(permission) {
return this.permissions.includes(permission) || this.role === 'admin';
};
userSchema.methods.updateLastActive = function() {
this.lastActive = Date.now();
return this.save({ validateBeforeSave: false });
};
export default mongoose.model('User', userSchema);
Advanced Security Implementation
Rate Limiting and DDoS Protection
// middleware/security.js - Advanced security middleware
import rateLimit from 'express-rate-limit';
import slowDown from 'express-slow-down';
import MongoStore from 'connect-mongo';
import session from 'express-session';
import csrf from 'csurf';
import { createHash } from 'crypto';
// Adaptive rate limiting based on endpoint sensitivity
export const createAdaptiveRateLimit = (options = {}) => {
const defaultOptions = {
windowMs: 15 * 60 * 1000, // 15 minutes
standardHeaders: true,
legacyHeaders: false,
handler: (req, res) => {
const retryAfter = Math.round(options.windowMs / 1000) || 900;
res.status(429).json({
error: 'Too many requests',
message: 'Please try again later',
retryAfter,
type: 'RateLimitError'
});
}
};
const sensitiveEndpoints = {
'/api/v1/auth/login': { max: 5, windowMs: 15 * 60 * 1000 },
'/api/v1/auth/register': { max: 3, windowMs: 60 * 60 * 1000 },
'/api/v1/auth/forgot-password': { max: 3, windowMs: 60 * 60 * 1000 },
'/api/v1/auth/reset-password': { max: 5, windowMs: 15 * 60 * 1000 },
'/api/v1/users': { max: 100, windowMs: 15 * 60 * 1000 },
'/api/v1/upload': { max: 20, windowMs: 15 * 60 * 1000 }
};
return (req, res, next) => {
const endpoint = req.route?.path;
const config = sensitiveEndpoints[endpoint] || options;
const limiter = rateLimit({
...defaultOptions,
max: config.max || options.max || 100,
windowMs: config.windowMs || options.windowMs || 15 * 60 * 1000,
keyGenerator: (req) => {
// Use IP + User Agent for better identification
const ip = req.ip || req.connection.remoteAddress;
const userAgent = req.get('User-Agent') || '';
return createHash('sha256').update(`${ip}:${userAgent}`).digest('hex');
},
skip: (req) => {
// Skip rate limiting for whitelisted IPs
const whitelistedIPs = process.env.WHITELISTED_IPS?.split(',') || [];
return whitelistedIPs.includes(req.ip);
}
});
limiter(req, res, next);
};
};
// Progressive delay for suspicious activity
export const progressiveDelay = slowDown({
windowMs: 15 * 60 * 1000, // 15 minutes
delayAfter: 10, // Allow 10 requests per window without delay
delayMs: 500, // Add 500ms delay per request after delayAfter
maxDelayMs: 20000, // Maximum delay of 20 seconds
skipFailedRequests: false,
skipSuccessfulRequests: false,
keyGenerator: (req) => {
return req.ip + ':' + (req.user?.id || 'anonymous');
}
});
// Session configuration with MongoDB store
export const sessionConfig = {
secret: process.env.SESSION_SECRET,
resave: false,
saveUninitialized: false,
store: MongoStore.create({
mongoUrl: process.env.DATABASE_URL,
touchAfter: 24 * 3600, // Only update session every 24 hours
ttl: 14 * 24 * 60 * 60, // 14 days
autoRemove: 'native'
}),
cookie: {
secure: process.env.NODE_ENV === 'production',
httpOnly: true,
maxAge: 14 * 24 * 60 * 60 * 1000, // 14 days
sameSite: 'strict'
},
name: 'sessionId', // Don't use default session name
genid: () => {
return createHash('sha256')
.update(Date.now() + Math.random().toString())
.digest('hex');
}
};
// CSRF protection
export const csrfProtection = csrf({
cookie: {
httpOnly: true,
secure: process.env.NODE_ENV === 'production',
sameSite: 'strict'
},
ignoreMethods: ['GET', 'HEAD', 'OPTIONS'],
value: (req) => {
return req.body._csrf ||
req.query._csrf ||
req.headers['x-csrf-token'] ||
req.headers['x-xsrf-token'];
}
});
// Input validation and sanitization
export const sanitizeInput = (req, res, next) => {
const sanitizeValue = (value) => {
if (typeof value === 'string') {
// Remove potential XSS attacks
return value.replace(/<script\b[^<]*(?:(?!<\/script>)<[^<]*)*<\/script>/gi, '')
.replace(/javascript:/gi, '')
.replace/on\w+\s*=/gi, '');
}
if (typeof value === 'object' && value !== null) {
const sanitized = Array.isArray(value) ? [] : {};
for (const key in value) {
sanitized[key] = sanitizeValue(value[key]);
}
return sanitized;
}
return value;
};
if (req.body) req.body = sanitizeValue(req.body);
if (req.query) req.query = sanitizeValue(req.query);
if (req.params) req.params = sanitizeValue(req.params);
next();
};
// Advanced request logging
export const securityLogger = (req, res, next) => {
const startTime = Date.now();
// Log security-relevant information
const securityInfo = {
ip: req.ip,
userAgent: req.get('User-Agent'),
method: req.method,
url: req.url,
timestamp: new Date().toISOString(),
headers: {
authorization: req.get('Authorization') ? '[REDACTED]' : undefined,
cookie: req.get('Cookie') ? '[REDACTED]' : undefined,
'x-forwarded-for': req.get('X-Forwarded-For'),
'x-real-ip': req.get('X-Real-IP')
}
};
res.on('finish', () => {
const duration = Date.now() - startTime;
const logEntry = {
...securityInfo,
statusCode: res.statusCode,
contentLength: res.get('Content-Length'),
duration,
userId: req.user?.id
};
// Log suspicious activity
if (res.statusCode === 401 || res.statusCode === 403 || res.statusCode === 429) {
console.warn('Security Event:', JSON.stringify(logEntry));
}
// Log slow requests
if (duration > 5000) {
console.warn('Slow Request:', JSON.stringify(logEntry));
}
});
next();
};
Performance Monitoring and Optimization
Comprehensive Performance Monitoring
// utils/performance.js - Performance monitoring and optimization
import { performance, PerformanceObserver } from 'perf_hooks';
import cluster from 'cluster';
import os from 'os';
import v8 from 'v8';
import EventEmitter from 'events';
class PerformanceMonitor extends EventEmitter {
constructor() {
super();
this.metrics = {
requests: {
total: 0,
successful: 0,
failed: 0,
pending: 0
},
response_times: {
min: Infinity,
max: 0,
avg: 0,
p95: 0,
p99: 0,
samples: []
},
memory: {
heap_used: 0,
heap_total: 0,
external: 0,
rss: 0
},
cpu: {
usage: 0,
load_average: []
},
errors: new Map(),
active_connections: 0
};
this.startTime = Date.now();
this.setupPerformanceObserver();
this.setupPeriodicChecks();
}
setupPerformanceObserver() {
const obs = new PerformanceObserver((list) => {
for (const entry of list.getEntries()) {
if (entry.entryType === 'measure') {
this.recordResponseTime(entry.duration);
}
}
});
obs.observe({ entryTypes: ['measure'] });
}
setupPeriodicChecks() {
// Memory monitoring every 30 seconds
setInterval(() => this.updateMemoryMetrics(), 30000);
// CPU monitoring every 60 seconds
setInterval(() => this.updateCpuMetrics(), 60000);
// Cleanup old samples every 5 minutes
setInterval(() => this.cleanupOldSamples(), 5 * 60 * 1000);
// Garbage collection monitoring
if (cluster.isMaster) {
setInterval(() => this.checkGarbageCollection(), 60000);
}
}
middleware() {
return (req, res, next) => {
const startMark = `req-start-${req.id || Date.now()}-${Math.random()}`;
const endMark = `req-end-${startMark}`;
performance.mark(startMark);
this.metrics.requests.pending++;
res.on('finish', () => {
performance.mark(endMark);
performance.measure(`request-${startMark}`, startMark, endMark);
this.metrics.requests.total++;
this.metrics.requests.pending--;
if (res.statusCode < 400) {
this.metrics.requests.successful++;
} else {
this.metrics.requests.failed++;
this.recordError(res.statusCode, req.url);
}
});
next();
};
}
recordResponseTime(duration) {
const samples = this.metrics.response_times.samples;
samples.push(duration);
// Keep only last 1000 samples
if (samples.length > 1000) {
samples.shift();
}
this.updateResponseTimeMetrics();
}
updateResponseTimeMetrics() {
const samples = this.metrics.response_times.samples;
if (samples.length === 0) return;
const sorted = [...samples].sort((a, b) => a - b);
const sum = samples.reduce((a, b) => a + b, 0);
this.metrics.response_times.min = Math.min(...samples);
this.metrics.response_times.max = Math.max(...samples);
this.metrics.response_times.avg = sum / samples.length;
this.metrics.response_times.p95 = this.getPercentile(sorted, 0.95);
this.metrics.response_times.p99 = this.getPercentile(sorted, 0.99);
}
getPercentile(sortedArray, percentile) {
const index = Math.ceil(sortedArray.length * percentile) - 1;
return sortedArray[Math.max(0, index)];
}
updateMemoryMetrics() {
const memUsage = process.memoryUsage();
const heapStats = v8.getHeapStatistics();
this.metrics.memory = {
heap_used: memUsage.heapUsed,
heap_total: memUsage.heapTotal,
external: memUsage.external,
rss: memUsage.rss,
heap_limit: heapStats.heap_size_limit
};
// Alert on high memory usage
const heapUsagePercent = (memUsage.heapUsed / heapStats.heap_size_limit) * 100;
if (heapUsagePercent > 90) {
this.emit('high-memory', heapUsagePercent);
}
}
updateCpuMetrics() {
this.metrics.cpu.load_average = os.loadavg();
// Calculate CPU usage
const cpus = os.cpus();
let totalIdle = 0;
let totalTick = 0;
cpus.forEach(cpu => {
for (const type in cpu.times) {
totalTick += cpu.times[type];
}
totalIdle += cpu.times.idle;
});
const idle = totalIdle / cpus.length;
const total = totalTick / cpus.length;
this.metrics.cpu.usage = 100 - ~~(100 * idle / total);
}
recordError(statusCode, url) {
const key = `${statusCode}:${url}`;
const current = this.metrics.errors.get(key) || { count: 0, lastSeen: null };
this.metrics.errors.set(key, {
count: current.count + 1,
lastSeen: Date.now()
});
}
cleanupOldSamples() {
const cutoff = Date.now() - (10 * 60 * 1000); // 10 minutes
for (const [key, error] of this.metrics.errors.entries()) {
if (error.lastSeen < cutoff) {
this.metrics.errors.delete(key);
}
}
}
checkGarbageCollection() {
const before = process.memoryUsage();
const start = performance.now();
if (global.gc) {
global.gc();
const after = process.memoryUsage();
const duration = performance.now() - start;
console.log('GC Stats:', {
duration: `${duration.toFixed(2)}ms`,
freed: `${Math.round((before.heapUsed - after.heapUsed) / 1024 / 1024)}MB`,
heapBefore: `${Math.round(before.heapUsed / 1024 / 1024)}MB`,
heapAfter: `${Math.round(after.heapUsed / 1024 / 1024)}MB`
});
}
}
getMetrics() {
return {
uptime: Date.now() - this.startTime,
timestamp: Date.now(),
process: {
pid: process.pid,
node_version: process.version,
platform: process.platform
},
...this.metrics,
system: {
load_average: os.loadavg(),
free_memory: os.freemem(),
total_memory: os.totalmem(),
cpu_count: os.cpus().length
}
};
}
reset() {
this.metrics.requests.total = 0;
this.metrics.requests.successful = 0;
this.metrics.requests.failed = 0;
this.metrics.response_times.samples = [];
this.metrics.errors.clear();
this.startTime = Date.now();
}
// Express route for metrics endpoint
metricsEndpoint() {
return (req, res) => {
res.json(this.getMetrics());
};
}
}
// Export singleton instance
export default new PerformanceMonitor();
// Usage in Express app
/*
import performanceMonitor from './utils/performance.js';
// Add middleware
app.use(performanceMonitor.middleware());
// Add metrics endpoint
app.get('/metrics', performanceMonitor.metricsEndpoint());
// Listen for alerts
performanceMonitor.on('high-memory', (percentage) => {
console.warn(`High memory usage detected: ${percentage.toFixed(2)}%`);
// Send alert to monitoring service
});
*/
Load Balancing and High Availability
Nginx Configuration for Express.js
# nginx.conf - Production Nginx configuration
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
events {
worker_connections 2048;
use epoll;
multi_accept on;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
# Logging
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for" '
'rt=$request_time uct="$upstream_connect_time" '
'uht="$upstream_header_time" urt="$upstream_response_time"';
access_log /var/log/nginx/access.log main;
# Basic Settings
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
server_tokens off;
# Security Headers
add_header X-Frame-Options DENY always;
add_header X-Content-Type-Options nosniff always;
add_header X-XSS-Protection "1; mode=block" always;
add_header Strict-Transport-Security "max-age=31536000; includeSubDomains; preload" always;
add_header Content-Security-Policy "default-src 'self'; script-src 'self' 'unsafe-inline'; style-src 'self' 'unsafe-inline';" always;
# Compression
gzip on;
gzip_vary on;
gzip_min_length 1000;
gzip_comp_level 6;
gzip_types
text/plain
text/css
text/xml
text/javascript
application/json
application/javascript
application/xml+rss
application/atom+xml
image/svg+xml;
# Rate Limiting
limit_req_zone $binary_remote_addr zone=api:10m rate=10r/s;
limit_req_zone $binary_remote_addr zone=auth:10m rate=1r/s;
limit_conn_zone $binary_remote_addr zone=conn_limit_per_ip:10m;
# Upstream servers
upstream express_backend {
least_conn;
server app1:3000 max_fails=3 fail_timeout=30s;
server app2:3000 max_fails=3 fail_timeout=30s;
server app3:3000 max_fails=3 fail_timeout=30s;
keepalive 32;
}
# Health check upstream
upstream express_health {
server app1:3000;
server app2:3000;
server app3:3000;
}
# SSL configuration
ssl_protocols TLSv1.2 TLSv1.3;
ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-RSA-AES128-SHA256:ECDHE-RSA-AES256-SHA384:ECDHE-RSA-AES128-SHA:ECDHE-RSA-AES256-SHA:DHE-RSA-AES128-SHA256:DHE-RSA-AES128-SHA:DHE-RSA-AES256-SHA256:DHE-RSA-AES256-SHA;
ssl_prefer_server_ciphers off;
ssl_session_cache shared:SSL:50m;
ssl_session_timeout 1d;
ssl_session_tickets off;
ssl_stapling on;
ssl_stapling_verify on;
# Main server configuration
server {
listen 80;
server_name api.yourdomain.com;
return 301 https://$server_name$request_uri;
}
server {
listen 443 ssl http2;
server_name api.yourdomain.com;
ssl_certificate /etc/nginx/ssl/fullchain.pem;
ssl_certificate_key /etc/nginx/ssl/privkey.pem;
# Security
limit_conn conn_limit_per_ip 20;
# Health check endpoint (no rate limiting)
location /health {
proxy_pass http://express_health;
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_connect_timeout 1s;
proxy_send_timeout 1s;
proxy_read_timeout 1s;
}
# Auth endpoints with strict rate limiting
location /api/v1/auth/ {
limit_req zone=auth burst=5 nodelay;
proxy_pass http://express_backend;
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_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_cache_bypass $http_upgrade;
proxy_connect_timeout 5s;
proxy_send_timeout 10s;
proxy_read_timeout 10s;
}
# API endpoints with moderate rate limiting
location /api/ {
limit_req zone=api burst=20 nodelay;
proxy_pass http://express_backend;
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_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_cache_bypass $http_upgrade;
proxy_connect_timeout 5s;
proxy_send_timeout 30s;
proxy_read_timeout 30s;
# Buffer settings
proxy_buffering on;
proxy_buffer_size 128k;
proxy_buffers 4 256k;
proxy_busy_buffers_size 256k;
}
# WebSocket support
location /socket.io/ {
proxy_pass http://express_backend;
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;
proxy_read_timeout 86400;
}
# Static files with caching
location ~* \.(js|css|png|jpg|jpeg|gif|ico|svg|woff|woff2|ttf|eot)$ {
proxy_pass http://express_backend;
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;
expires 1y;
add_header Cache-Control "public, immutable";
}
# Default fallback
location / {
proxy_pass http://express_backend;
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_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_cache_bypass $http_upgrade;
}
# Error pages
error_page 404 /404.html;
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
}
}
How do I deploy Express with an AI agent?
Everything above is a lot of setup for each server. CloudPloy runs the repeatable parts for you: it builds Node apps with Nixpacks, issues HTTPS certificates and switches releases with zero downtime. Your AI agent triggers it over MCP.
- Connect a server. Use an AWS account you own, or any fresh Ubuntu or Debian server you can reach over SSH.
- Add the MCP server to your agent. In Claude Code:
claude mcp add --transport http cloudploy https://brain.cloudploy.com/mcp. Cursor, Codex and ChatGPT use the same address. See the Claude Code guide. - Ask: “Create an app from
my-org/my-express-apiand deploy it.” SetDATABASE_URL,JWT_SECRETand your other variables withset_env_vars. - Turn on auto-deploy for
main, and add your domain. The agent provisions the certificate.
CloudPloy can run Redis (Valkey), PostgreSQL or MySQL on your server and link it to the app, which gets REDIS_URL or DATABASE_URL. It does not run MongoDB: keep that on a database service or a server you manage, and point the app at it with an environment variable. There is a Free plan for one small server and one app; paid plans are $9.99 to $39 a month. See pricing and the MCP docs.
Production checklist
- ✅ Enable clustering with PM2
- ✅ Use production-grade middleware stack
- ✅ Implement comprehensive caching strategy
- ✅ Configure proper error handling
- ✅ Set up performance monitoring
- ✅ Use connection pooling for databases
- ✅ Enable compression and static file optimization
- ✅ Implement rate limiting and security measures
- ✅ Configure proper logging and metrics
- ✅ Set up health checks and graceful shutdown
FAQ
How do I deploy an Express.js app to production?
Run it under PM2 or in Docker, put Nginx in front as a reverse proxy, add a Let’s Encrypt certificate, and set secrets as environment variables. The sections above have the config for each step.
Do I need PM2 if I use Docker?
No. Docker’s restart policy restarts a crashed container, so you can run node directly inside it. PM2 is useful when you run the app straight on the server, where it handles restarts and clustering.
Which Node.js version should I use for Express?
Use an LTS release: Node 24 or 26 for new servers. Node 22 is supported until April 30, 2027, and Node 20 is already end of life.
Can an AI agent deploy my Express app?
Yes. Claude Code, Cursor and other MCP clients can call CloudPloy to create the app, set environment variables, deploy and roll back. Setup is in the section above.
Do I need Nginx in front of Express?
It is the usual choice. Nginx handles HTTPS, compression and static files so Node only runs your code. CloudPloy sets up routing and certificates for you if you deploy that way.
Conclusion
An Express.js deployment that holds up needs a process manager, a reverse proxy, HTTPS, locked-down secrets and monitoring. Set those up by hand with the sections above, or connect a server to CloudPloy and have your agent do the repeatable parts.
Updated November 23, 2026.