MohxNotes

Deploy Node.js API and Set Up Nginx Reverse Proxy: Your Web Traffic Control Tower! 🚦

Mohammed3 Min ReadVPS and Nginx Deployment Β· Part 5
Deploy Node.js API and Set Up Nginx Reverse Proxy: Your Web Traffic Control Tower! 🚦

Hey there, web architects! πŸ‘‹ Ready to build your own internet skyscraper? Let’s deploy a Node.js API and set up Nginx as a reverse proxy. It’s like creating your own web traffic control tower! 🏒

Part 1: Building Our Express Skyscraper πŸ—οΈ

Step 1: Get Your Building Materials (Install Node.js and npm) 🧱

First, let’s get our tools ready. Open your terminal and type:

sudo apt install nodejs npm

Step 2: Create Your Construction Site 🚧

Let’s make a spot for our new building:

mkdir express-app
cd express-app
npm init -y

Step 3: Gather More Supplies (Install Dependencies) πŸ› οΈ

Time to get some special building blocks:

npm install express nodemon

Step 4: Draw Up the Blueprints (Update package.json) πŸ“

Open your package.json and make it look like this:

{
  "name": "express-app",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "type": "module",
  "scripts": {
    "dev": "nodemon index.js"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "dependencies": {
    "express": "^4.19.2",
    "nodemon": "^3.1.4"
  }
}

Step 5: Build Your Express Elevator (Create index.js) πŸ›—

Create a new file called index.js and add this code:

import express from "express";
 
const app = express();
 
app.get("/api", (req, res) => {
  return res.status(200).json({
    message: "server working",
  });
});
 
app.listen(5000, () => console.log("Server is running on 5000"));

Step 6: Hire a Building Manager (Install PM2) πŸ‘·

PM2 will keep our building running 24/7:

npm install -g pm2

Step 7: Open for Business! πŸŽ‰

Start your new skyscraper:

pm2 start "npm run dev"

Part 2: Setting Up Your Traffic Control Tower (Nginx Reverse Proxy) πŸ—Ό

Step 1: Configure Your Control Tower πŸ”§

Open your Nginx config file:

sudo nano /etc/nginx/sites-available/default

Step 2: Program Your Traffic Controller 🚦

Replace the content with this:

server {
    listen 80;
    server_name localhost;
 
    location / {
        proxy_pass http://127.0.0.1:5000;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
    }
}

Step 3: Test and Launch Your Control Tower πŸš€

Check if everything’s okay:

sudo nginx -t

If it says β€œOK”, let’s start directing traffic:

sudo systemctl reload nginx

You Did It! Your Web Skyscraper is Open for Business! 🎊

Congratulations, web architect! You’ve just built a high-tech API skyscraper with its own traffic control tower. Now when someone visits http://<your-server-ip>/api, they’ll get a warm welcome message:

{ "message": "server working" }

Your Express app is now running behind Nginx, protected and ready to handle tons of visitors. Keep building and reaching for the (web) stars! πŸŒŸπŸ™οΈ