Hosting Node.js Applications
Prepare a Node.js service for deployment behind a process manager and reverse proxy.
Lesson content
🎯 Goal By the end of this lesson, you'll understand: What is Hosting? Why do we host Node.js applications? Types of Hosting Deployment Process Popular Hosting Platforms Production Best Practices Interview Questions What is Hosting? Hosting means making your application available on the internet so users can access it from anywhere. Simple Definition: Hosting is the process of deploying your application to a server so users can use it online. Why Do We Need Hosting? Without hosting: Application works only on your computer. Other users cannot access it. With hosting: Users can access it from anywhere. Application runs 24×7. Supports multiple users. Real-Life Example 🏠 Imagine opening a restaurant. Cooking at Home │ ▼ Only Family Can Eat Restaurant │ ▼ Everyone Can Visit Your computer = Home Hosted Server = Restaurant Development vs Production Development Production Local Computer Live Server Used by Developers Used by Real Users Debugging Enabled Optimized & Secure localhost Public Domain/IP Hosting Process Develop Application │ ▼ Test Application │ ▼ Upload to Server │ ▼ Install Dependencies │ ▼ Start Application │ ▼ Application Live Popular Hosting Platforms Platform Best For Render Beginners Railway Small Projects DigitalOcean VPS Hosting AWS EC2 Large Applications Microsoft Azure Enterprise Google Cloud Cloud Applications Heroku (where available) Learning & Small Apps Vercel Frontend + Serverless Functions Netlify Frontend + Serverless Functions Note: Traditional Node.js servers are commonly hosted on platforms like Render, Railway, VPS providers, or cloud virtual machines. Vercel and Netlify are primarily designed for frontend apps and serverless functions. Node.js Hosting Diagram Developer │ ▼ GitHub │ ▼ Hosting Platform │ ▼ Internet │ ▼ Users Prepare Your Project Install dependencies. npm install package.json Ensure a start script exists. { "scripts": { "start": "node app.js" } } Environment Variables Never hardcode secrets. ❌ Bad const PORT = 3000 ; const DB_PASSWORD = "mypassword" ; ✅ Good const PORT = process . env . PORT || 3000 ; const DB_PASSWORD = process . env . DB_PASSWORD; .env PORT=3000 DB_PASSWORD=secret JWT_SECRET=mySecretKey Listen on the Correct Port const PORT = process . env . PORT || 3000 ; app . listen( PORT , () => { console . log( `Server Running on ${ PORT } ` ); }); Use PM2 Install PM2. npm install -g pm2 Start Application. pm2 start app.js View Running Apps. pm2 list Restart Application. pm2 restart app Stop Application. pm2 stop app Reverse Proxy (Nginx) Internet │ ▼ Nginx │ ▼ Node.js Application Benefits: SSL Termination Load Balancing Reverse Proxy Better Performance Domain Name Instead of: http://192.168.1.10:3000 Use: https://example.com Benefits: Easy to remember Professional Secure with HTTPS HTTPS Install an SSL certificate. HTTP ↓ HTTPS Benefits: Secure Communication Data Encryption Trusted by Browsers Deployment Flow Code │ ▼ GitHub │ ▼ Hosting Platform │ ▼ Build │ ▼ Deploy │ ▼ Live Website CI/CD (Continuous Integration & Continuous Deployment) CI/CD automatically builds and deploys your application whenever you push code. Developer │ ▼ Git Push │ ▼ GitHub │ ▼ CI/CD Pipeline │ ▼ Build │ ▼ Deploy │ ▼ Production Popular tools: GitHub Actions GitLab CI/CD Jenkins Azure DevOps Folder Structure project/ │ ├── src/ ├── public/ ├── package.json ├── .env ├── .gitignore └── app.js .gitignore node_modules/ .env logs/ Production Best Practices ✅ Use environment variables. ✅ Enable HTTPS. ✅ Use PM2 or another process manager. ✅ Use Nginx as a reverse proxy. ✅ Keep dependencies updated. ✅ Monitor application logs. ✅ Enable automatic backups. ✅ Configure CI/CD. Hosting Example User │ ▼ https://example.com │ ▼ Nginx │ ▼ PM2 │ ▼ Node.js │ ▼ MongoDB / MySQL Real-Life Example 📦 Online Shopping Website Developer │ ▼ Build Shopping App │ ▼ Upload to Cloud Server │ ▼ Website Goes Live │ ▼ Customers Can Order Products Interview Question What is Hosting? Answer Hosting is the process of deploying an application to a server so users can access it over the internet. Why do we use process.env.PORT ? Answer Because hosting providers assign the port number dynamically, and your application should listen on the port provided by the environment. Why is PM2 used? Answer PM2 is used to keep Node.js applications running, automatically restart them after crashes, manage logs, and support zero-downtime reloads. Why do we use Nginx with Node.js? Answer Nginx acts as a reverse proxy, can terminate HTTPS, serve static files efficiently, and distribute traffic to one or more Node.js instances. What is CI/CD? Answer CI/CD is an automated process that builds, tests, and deploys applications whenever code changes are pushed. Best Practices ✅ Store secrets in environment variables. ✅ Always use HTTPS in production. ✅ Use a process manager like PM2. ✅ Configure automatic deployments with CI/CD. ✅ Monitor logs and application health. ✅ Keep dependencies up to date. ❌ Never commit the .env file to Git. ❌ Don't expose sensitive information in source code. ❌ Don't run production applications using only node app.js . Key Points ✅ Hosting makes your application available on the internet. ✅ Production environments are different from development. ✅ Use process.env.PORT for compatibility with hosting platforms. ✅ PM2 helps manage Node.js processes. ✅ Nginx improves security and performance. ✅ CI/CD automates deployment. ✅ HTTPS protects data between users and your server. 🧠 Easy Formula Develop App │ ▼ GitHub │ ▼ Hosting Platform │ ▼ PM2 │ ▼ Nginx │ ▼ HTTPS │ ▼ Users Hosting = Live Application PM2 = Process Manager Nginx = Reverse Proxy HTTPS = Secure Website CI/CD = Automatic Deployment Remember: 🌍 Hosting = Make your app available online 🚀 PM2 = Keeps your Node.js app running 🔀 Nginx = Reverse proxy and HTTPS support 🔒 HTTPS = Secure communication ⚙️ CI/CD = Automatically build and deploy your application ☁️ Cloud platforms like Render, Railway, AWS, Azure, and Google Cloud are common choices for hosting Node.js applications