Chapter 6: Deployment
Your API works perfectly on your laptop. You hit "run" and Postman returns the exact JSON you want. Congratulations! But a web server running on localhost:3000 is completely useless to the rest of the world.
You must move your code from your laptop to a server in the cloud that stays on 24/7. This transition is known as Deployment.
VPS vs Serverless (PaaS)
There are two main ways to host a backend application today:
- Virtual Private Servers (VPS): Companies like DigitalOcean or AWS EC2 give you a blank Linux computer. You SSH into it via a terminal, install Node.js yourself, clone your Git repository, and run the server. Maximum control, maximum effort.
- Platform as a Service (PaaS): Companies like Render, Heroku, or Vercel. You simply connect your GitHub repository to them, and they automatically install the environment, build your code, and host it. Less control, but incredibly fast and easy.
Docker: "It works on my machine"
The most famous bug in software engineering: "It works on my laptop, but it crashes on the cloud server!" This happens because your laptop and the server have different operating systems, different versions of Node, and different environment variables.
Docker solves this. You write a Dockerfile that packages your code, the operating system, and all dependencies into a single "Container". This container will run exactly the same way on a Mac, Windows, or a Linux cloud server.
# A standard Dockerfile for a Node.js API
FROM node:18-alpine
WORKDIR /app
COPY package*.json ./
RUN npm install
COPY . .
EXPOSE 3000
CMD ["npm", "start"]
CI/CD Pipelines
CI/CD stands for Continuous Integration / Continuous Deployment. Instead of manually logging into your server and dragging files over every time you make an update, you set up a pipeline (like GitHub Actions). When you push code to the `main` branch, the pipeline automatically runs tests and deploys the new code to the live server.
Mini Task: Free Cloud Hosting
- Create an account on Render.com or Railway.app.
- Connect your GitHub account.
- Deploy a simple Node.js "Hello World" API repository. You will get a live URL to share with the world in less than 5 minutes!