
Hosting a Node.js Express RESTful API on an Ubuntu server is a great way to deploy a scalable and secure backend for your applications. Pairing it with Nginx as a reverse proxy ensures better performance, scalability, and ease of handling requests.
In this guide, we’ll walk you through the process step by step. If you don’t have a server yet, check out Hostomy Cloud Servers to get started with reliable hosting.
Update the package lists to ensure you have the latest versions:
sudo apt update && sudo apt upgrade -y
If Node.js isn’t installed, you can install it using NVM (Node Version Manager):
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.5/install.sh | bash source ~/.bashrc nvm install node
Verify the installation:
node -v npm -v
Detailed guide: How to Install NodeJS On Ubuntu
Create a directory for your application:
mkdir my-api && cd my-api
Initialize a Node.js project:
npm init -y
Install Express:
npm install express
Create your API file:
nano index.js
Add the following code to index.js:
const express = require('express');
const app = express();
const port = 3000;
app.use(express.json());
app.get('/', (req, res) => {
res.send('Welcome to my RESTful API!');
});
app.listen(port, () => {
console.log(`API running at http://localhost:${port}`);
});
Start the API to ensure it works:
node index.js
Visit http://<server_ip>:3000 in your browser or use curl:
curl http://localhost:3000
To keep your API running in the background, install PM2:
sudo npm install -g pm2
Start your application with PM2:
pm2 start index.js
Set PM2 to start on boot:
pm2 startup pm2 save
Detailed guide: How to Install PM2 on Ubuntu
Install Nginx:
sudo apt install nginx -y
Create a new Nginx configuration file:
sudo nano /etc/nginx/sites-available/my-api
Add the following configuration:
server {
listen 80;
server_name your_domain_or_ip;
location / {
proxy_pass http://localhost:3000;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
}
Enable the configuration:
sudo ln -s /etc/nginx/sites-available/my-api /etc/nginx/sites-enabled/
Test the Nginx configuration for syntax errors:
sudo nginx -t
Reload Nginx:
sudo systemctl reload nginx
Now, you can access your API at http://your_domain_or_ip.
For better security, secure your API with SSL using Let’s Encrypt:
Install Certbot:
sudo apt install certbot python3-certbot-nginx -y
Obtain an SSL certificate:
sudo certbot --nginx -d your_domain_or_ip
Verify auto-renewal:
sudo certbot renew --dry-run
Now your API is accessible over HTTPS!
Hostomy offers affordable, high-performance cloud servers starting at just ₹145/month. With excellent uptime and scalability, it’s the perfect platform to host your Node.js applications. Check out Hostomy Cloud Servers today.
Hosting a Node.js Express RESTful API on Ubuntu with Nginx as a reverse proxy ensures a secure, scalable, and reliable setup. This guide covers all the steps to get your API up and running. Pair it with a Hostomy Cloud Server for the best hosting experience!