Deploying Express Apps
Getting an Express app into production - environment configuration, Docker, process managers, platform choices, and the production-readiness checklist.
What it is, in plain English: Deploying an Express app means making it run reliably on a server accessible from the internet -- with the right environment configuration, the ability to restart automatically if it crashes, and (usually) a process to run multiple instances for reliability and to use all available CPU cores. Platforms like Railway and Render handle most of this automatically; Docker plus a process manager gives full control for self-managed or cloud-provider deployments.
Getting from "works on my machine" to production
Environment variables -- the configuration pattern
// Never hardcode config that differs between environments:
const PORT = process.env.PORT || 3000;
const DB_URL = process.env.DATABASE_URL;
const NODE_ENV = process.env.NODE_ENV || "development";
if (!DB_URL) {
throw new Error("DATABASE_URL environment variable is required");
// fail loudly at startup rather than mysteriously later
}
// .env file for local development (NEVER commit this):
// PORT=3000
// DATABASE_URL=postgresql://localhost/myapp_dev
// JWT_SECRET=dev-secret-not-for-production
// npm install dotenv, then at the top of your entry file:
import "dotenv/config";A health check endpoint
// Every hosting platform and container orchestrator polls a health
// endpoint to know if your app is alive and ready for traffic:
app.get("/health", (req, res) => {
res.json({ status: "ok", uptime: process.uptime() });
});
// More thorough version -- checks dependencies too:
app.get("/health", async (req, res) => {
try {
await prisma.$queryRaw`SELECT 1`; // verify DB is reachable
res.json({ status: "ok", database: "connected" });
} catch {
res.status(503).json({ status: "error", database: "disconnected" });
}
});Simple platform deployment (Railway/Render)
# These platforms detect a Node.js app automatically from package.json:
# {
# "scripts": { "start": "node server.js" }
# }
#
# 1. Connect your GitHub repo
# 2. Set environment variables in the platform dashboard
# 3. git push -- platform builds and deploys automatically
# 4. Get a public URL instantly, with automatic HTTPSDockerizing an Express app
# Dockerfile
FROM node:20-alpine
WORKDIR /app
COPY package*.json ./
RUN npm ci --omit=dev
COPY . .
EXPOSE 3000
USER node
CMD ["node", "server.js"]
# Build and run:
docker build -t my-express-app .
docker run -p 3000:3000 --env-file .env my-express-app
Try It Yourself (Node.js logic)
Official Sources
- Express.js — Production Best Practices: Performance and Reliability
- PM2 Documentation
- Railway Documentation
- Docker Documentation — Node.js Guide
The Codex links only to official documentation. No third-party tutorials.