Deployment Strategies: Where does code live?

Deployment Strategies: Where does code live? in JavaScript: Writing code is only 50% of the job.

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.

  1. Shared Hosting: The "Cheap Apartment." You share a computer with others. Good for basic sites.
  2. VPS (Virtual Private Server): The "Small House." You have your own OS but it's on a shared physical machine. Good for small APIs.
  3. Docker/Containers: The "Shipping Container." You package your app and all its dependencies into a single box that runs the same way everywhere.
  4. 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

  1. The Dockerfile: Research a basic Dockerfile for Node.js. What do the FROM and COPY commands do?
  2. The Bodyguard: Install PM2 (if possible) and use pm2 start to run a script that deliberateity crashes after 5 seconds. Watch PM2 restart it!
  3. The Cold Start: Research what a "Cold Start" is in Serverless (AWS Lambda). How does it affect the first user who visits your site?