Express.js
Beginner
1 min read
Running Express in Production with PM2
Example
// ecosystem.config.js (committed to git, no secrets)
module.exports = {
apps: [
{
name: 'api',
script: './server.js',
instances: 'max', // one per CPU core
exec_mode: 'cluster',
watch: false, // disable in production
max_memory_restart: '512M',
env: {
NODE_ENV: 'development',
PORT: 3000,
},
env_production: {
NODE_ENV: 'production',
PORT: 8080,
},
error_file: './logs/err.log',
out_file: './logs/out.log',
log_date_format: 'YYYY-MM-DD HH:mm:ss',
},
],
};
// Commands:
// pm2 start ecosystem.config.js --env production
// pm2 reload api # zero-downtime reload
// pm2 logs api # tail logs
// pm2 monit # live dashboard
// pm2 startup # generate startup script
// pm2 save # save process list
Related Resources
Express.js Reference
Complete tag & property list
Express.js How-To Guides
Step-by-step practical guides
Express.js Exercises
Practice what you've learned
More in Express.js