📖 Scenario: You are building a simple Express server to serve a welcome message. You want to manage this server process efficiently using PM2, a popular process manager for Node.js applications.
🎯 Goal: Create a basic Express server, configure PM2 to manage it, and start the server using PM2 to keep it running smoothly.
📋 What You'll Learn
Create a basic Express server in app.js that listens on port 3000
Add a PM2 ecosystem configuration file ecosystem.config.js to define the app process
Use PM2 commands to start and manage the Express server
Ensure the server responds with 'Hello from Express with PM2!' on the root URL
💡 Why This Matters
🌍 Real World
PM2 is widely used in real projects to keep Node.js servers running smoothly, automatically restarting them if they crash and managing multiple processes easily.
💼 Career
Knowing how to use PM2 with Express is a valuable skill for backend developers and DevOps engineers to ensure reliable server uptime.
Progress0 / 4 steps
1
Create a basic Express server
Create a file app.js and write code to import express, create an app, and set it to listen on port 3000. Add a route for GET / that sends the text 'Hello from Express with PM2!'.
Express
Hint
Use import express from 'express' to import Express. Create the app with express(). Use app.get('/', ...) to handle the root route. Listen on port 3000.
2
Add PM2 ecosystem configuration
Create a file named ecosystem.config.js. Define an export with a apps array containing one app object. Set the name to 'express-app' and the script to 'app.js'.
Express
Hint
Use export default to export the config. Inside apps array, add an object with name and script properties.
3
Start the Express app using PM2
Write the PM2 command to start the app using the ecosystem config file ecosystem.config.js. Use the command pm2 start ecosystem.config.js.
Express
Hint
Use the terminal command pm2 start ecosystem.config.js to start the app with PM2.
4
Manage the Express app with PM2
Write the PM2 command to list all running processes. Use pm2 list to see the status of your Express app.
Express
Hint
Use the terminal command pm2 list to view all running PM2 processes.
Practice
(1/5)
1. What is the main purpose of using PM2 with an Express application?
easy
A. To write the Express app code faster
B. To style the Express app's frontend
C. To keep the app running continuously without manual restarts
D. To create database schemas automatically
Solution
Step 1: Understand PM2's role
PM2 is a process manager that keeps Node.js apps running smoothly by restarting them if they crash or stop.
Step 2: Identify its use with Express
Using PM2 with Express ensures the server stays online without manual restarts after crashes or updates.
Final Answer:
To keep the app running continuously without manual restarts -> Option C
Quick Check:
PM2 manages app uptime = B [OK]
Hint: PM2 keeps your app alive automatically [OK]
Common Mistakes:
Thinking PM2 helps with frontend styling
Confusing PM2 with code writing tools
Assuming PM2 manages databases
2. Which of the following is the correct command to start an Express app named app.js using PM2?
easy
A. pm2 run app.js
B. pm2 start app.js
C. pm2 launch app.js
D. pm2 execute app.js
Solution
Step 1: Recall PM2 start command
The correct PM2 command to launch an app is pm2 start <file>.
Step 2: Match the command with options
Among the options, only pm2 start app.js matches the correct syntax.
What is the state of the app after these commands?
medium
A. The app is running but without a name
B. The app is stopped but still managed by PM2
C. The app is running with the name 'myapp'
D. The app is removed from PM2 process list and not running
Solution
Step 1: Analyze each command effect
pm2 start app.js --name myapp starts the app named 'myapp'. pm2 restart myapp restarts it. pm2 stop myapp stops the app but keeps it in PM2 list. pm2 delete myapp removes the app from PM2 list and stops it completely.
Step 2: Determine final state
After deletion, the app is no longer managed by PM2 and is not running.
Final Answer:
The app is removed from PM2 process list and not running -> Option D
Quick Check:
Delete removes app from PM2 = A [OK]
Hint: Delete removes app from PM2 and stops it [OK]
Common Mistakes:
Thinking stop deletes the app from PM2
Assuming restart keeps app stopped
Confusing app running state after delete
4. You tried to start your Express app with PM2 using pm2 start app.js, but it immediately stops. Which of the following is the most likely cause?
medium
A. The app.js file has a syntax error causing it to crash
B. PM2 does not support Express apps
C. You need to use pm2 run instead of pm2 start
D. The app.js file is missing the package.json
Solution
Step 1: Understand PM2 behavior on crashes
If the app crashes immediately after start, PM2 will stop it and try to restart repeatedly.
Step 2: Identify common crash reasons
A syntax error in app.js is a common cause for immediate crash and stop.
Final Answer:
The app.js file has a syntax error causing it to crash -> Option A
Quick Check:
Crash causes immediate stop = C [OK]
Hint: Check app.js syntax if PM2 stops immediately [OK]
Common Mistakes:
Believing PM2 can't run Express apps
Using wrong PM2 commands like 'run'
Thinking package.json is required to start app.js
5. You want to run multiple instances of your Express app using PM2 to improve performance on a multi-core server. Which command correctly achieves this?
hard
A. pm2 start app.js -i max
B. pm2 start app.js --instances 1
C. pm2 start app.js --scale 4
D. pm2 start app.js --multi 4
Solution
Step 1: Understand PM2 instance options
PM2 uses -i or --instances to specify how many app instances to run. Using -i max runs as many instances as CPU cores.
Step 2: Evaluate options
--instances 1 runs only one instance. --scale and --multi are not valid PM2 options.
Final Answer:
pm2 start app.js -i max -> Option A
Quick Check:
Use '-i max' for multi-core instances = A [OK]
Hint: Use '-i max' to run all CPU cores [OK]
Common Mistakes:
Using invalid options like --scale or --multi
Setting instances to 1 for multi-core use
Confusing PM2 flags for scaling
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.