I am creating a web application using express.js. I have been following a course by Angela Wu, and one of the chapters involves a templating tool called 'Express' which is built on node.js. Following is a very simple example for a file named 'node.js':


import express from "express";

const app = express();
const port = 3000;

app.get("/", (req, res) => {
  res.send("<h1>Hello World!</h1>");
});

app.listen(port, () => {
  console.log(`Listening on port $(port)`)
});

To run this, I open a terminal window inside of Visual Studio Code, and execute the command

node index.js

This will then use the 'node' library to create an http listener on 'localhost/3000'.

Now here is the question: How can I deploy this simple application to a remote host?

1 Reply 1

you can use various approach. This is the most common production setup.

Step 1: SSH into your server

ssh user@your_server_ip

Step 2: Install Node & Git

sudo apt update
sudo apt install -y nodejs npm git

Step 3: Clone your project

git clone https://github.com/your/repo.git
cd repo
npm install

Step 4: Install PM2 (process manager)

PM2 keeps your Node app running in the background:

sudo npm install -g pm2
pm2 start server.js --name myapp
pm2 save
pm2 startup

Step 5: Configure Nginx as reverse proxy

Install Nginx:

sudo apt install nginx


Create a config file:

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


Add:

server {
    listen 80;
    server_name your_domain_or_ip;

    location / {
        proxy_pass http://localhost:3000;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
    }
}


Enable the site:

sudo ln -s /etc/nginx/sites-available/myapp /etc/nginx/sites-enabled/
sudo systemctl restart nginx

Your Reply

By clicking “Post Your Reply”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.