Concept Explanation
Writing code is only 50% of the job. The other 50% is putting that code on a computer that is always on and connected to the internet.
- Shared Hosting: The "Cheap Apartment." You share a computer with others. Good for basic sites.
- VPS (Virtual Private Server): The "Small House." You have your own OS but it's on a shared physical machine. Good for small APIs.
- Docker/Containers: The "Shipping Container." You package your app and all its dependencies into a single box that runs the same way everywhere.
- Serverless: The "Cloud Utility." You don't manage any server; you just upload a function and pay per use.
Technical Concept: PM2 (Process Manager)
When you run node index.js, and the script crashes, your website goes down. PM2 is a "Bodyguard" for your app. If it crashes, PM2 instantly restarts it. It can also manage multiple "Clusters" to use all your computer's CPU power.
Syntax or Core Structure
Simple PM2 commands
npm install pm2 -g
pm2 start index.js --name "my-api"
pm2 list # See all running apps
pm2 logs # Watch real-time activity
pm2 restart all # Refresh everything
Practice Exercises
- The Dockerfile: Research a basic
Dockerfilefor Node.js. What do theFROMandCOPYcommands do? - The Bodyguard: Install PM2 (if possible) and use
pm2 startto run a script that deliberateity crashes after 5 seconds. Watch PM2 restart it! - The Cold Start: Research what a "Cold Start" is in Serverless (AWS Lambda). How does it affect the first user who visits your site?