Deploy Node.js API and Set Up Nginx Reverse Proxy: Your Web Traffic Control Tower! ๐Ÿšฆ

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
Copied!

#Step 2: Create Your Construction Site ๐Ÿšง

Letโ€™s make a spot for our new building:

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

#Step 3: Gather More Supplies (Install Dependencies) ๐Ÿ› ๏ธ

Time to get some special building blocks:

npm install express nodemon
Copied!

#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"
  }
}
Copied!

#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"))
Copied!

#Step 6: Hire a Building Manager (Install PM2) ๐Ÿ‘ท

PM2 will keep our building running 24/7:

npm install -g pm2
Copied!

#Step 7: Open for Business! ๐ŸŽ‰

Start your new skyscraper:

pm2 start "npm run dev"
Copied!

#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
Copied!

#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;
    }
}
Copied!

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

Check if everythingโ€™s okay:

sudo nginx -t
Copied!

If it says โ€œOKโ€, letโ€™s start directing traffic:

sudo systemctl reload nginx
Copied!

#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" }
Copied!

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