Paramclasses Academy - Blog

Tutorials Details

←
Post 38 of 40
→

Deploy a Node.js Application: Hosting, Environment Setup and Production Tips

Deploy a Node.js Application: Hosting, Environment Setup and Production Tips

October 30, 2026

✦ Deploy a Node.js Application: Hosting, Environment Setup and Production Tips

✦ Introduction

Building a Node.js application is only the first step in web development. After completing development and testing on localhost, the next important step is deployment.

Node.js deployment means making your application available on the internet so users can access it through a public domain or server IP address.

For example, while developing an application, you may access it using:

http://localhost:3000

After deployment, users can access it through a live URL such as:

https://www.example.com

In this tutorial, we will learn how to prepare a Node.js application for production, configure environment variables, run it using PM2, and understand important deployment practices.

✦ 1. What Is Node.js Deployment?

Node.js deployment is the process of transferring your application from a development environment to a production server.

A production server is a computer or cloud platform that runs your application and makes it accessible to users.

A typical deployment process includes:

  • Preparing application files

  • Installing Node.js on the server

  • Installing project dependencies

  • Configuring environment variables

  • Connecting the production database

  • Starting the application

  • Configuring a domain and HTTPS

  • Monitoring application performance

✦ 2. Prepare Your Node.js Application

Before deploying your application, make sure your project has a proper structure.

Example project structure:

my-node-app/
│
├── node_modules/
├── public/
├── routes/
├── .env
├── .gitignore
├── package.json
├── package-lock.json
└── server.js

Create a basic Express application.

First, install Express:

npm install express

Now create a file named server.js.

const express = require('express');

const app = express();

const PORT = process.env.PORT || 3000;

app.get('/', (req, res) => {
    res.send('Welcome to My Node.js Application!');
});

app.get('/health', (req, res) => {
    res.json({
        status: 'OK',
        message: 'Application is running'
    });
});

app.listen(PORT, '0.0.0.0', () => {
    console.log(`Server running on port ${PORT}`);
});

Here:

  • express() creates an Express application.

  • process.env.PORT reads the port provided by the hosting platform.

  • / displays the home page.

  • /health provides a basic health-check endpoint.

  • app.listen() starts the server.

Important: Always use process.env.PORT in production because many hosting platforms assign the application port automatically.

✦ 3. Configure package.json

Your package.json file contains project information, dependencies and scripts.

Example:

{
  "name": "my-node-app",
  "version": "1.0.0",
  "description": "My Node.js production application",
  "main": "server.js",
  "scripts": {
    "start": "node server.js",
    "dev": "node --watch server.js"
  },
  "dependencies": {
    "express": "^5.1.0"
  }
}

You can start the application using:

npm start

For development, you can use:

npm run dev

Use a supported version of Node.js and keep your dependencies updated.

✦ 4. Understand Environment Variables

Environment variables store configuration values separately from application code.

Examples include:

  • Database username

  • Database password

  • API keys

  • Application port

  • JWT secret

  • Email credentials

Install dotenv:

npm install dotenv

Create a .env file:

PORT=3000
DB_HOST=localhost
DB_USER=root
DB_PASSWORD=your_password
DB_NAME=my_database
JWT_SECRET=your_long_random_secret

Load these values at the beginning of server.js:

require('dotenv').config();

const express = require('express');

const app = express();

const PORT = process.env.PORT || 3000;

app.get('/', (req, res) => {
    res.send('Node.js application is running!');
});

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

Now you can access environment variables using:

console.log(process.env.DB_HOST);

Security Tip: Never upload your .env file to a public GitHub repository. Add it to .gitignore.

Example .gitignore:

node_modules/
.env
logs/

On hosting platforms, configure production environment variables through their environment settings rather than uploading your local .env file.

✦ 5. Choose a Hosting Platform

You need a server or hosting platform to make your Node.js application live.

Common deployment options include:

1. VPS Hosting

A Virtual Private Server gives you control over the server environment. You can install Node.js, configure Nginx, manage databases and run applications.

2. Cloud Application Platforms

Platforms such as Render and Railway provide application deployment workflows that can simplify hosting and server management.

3. Cloud Infrastructure

Cloud providers such as AWS, Google Cloud and Microsoft Azure offer infrastructure for applications that require more control and scalability.

Choose hosting based on your budget, application traffic, database requirements and technical experience.

✦ 6. Deploy Node.js on a Linux VPS

Let's understand a basic VPS deployment workflow.

First, connect to your server using SSH:

ssh username@YOUR_SERVER_IP

Update the server packages:

sudo apt update
sudo apt upgrade -y

Install Node.js using a supported installation method for your Linux distribution. Verify the installation:

node -v
npm -v

Now upload or clone your project to the server.

For example, using Git:

git clone YOUR_REPOSITORY_URL

Move into your project directory:

cd my-node-app

Install production dependencies:

npm ci --omit=dev

Configure the production environment variables, then start the application:

npm start

Your application should now be running on the configured port.

For a production server, do not depend on an open SSH terminal to keep the application running. Use a process manager.

✦ 7. Run Node.js Using PM2

PM2 is a process manager for Node.js applications. It helps keep your application running and can restart it if the process crashes.

Install PM2 globally:

npm install -g pm2

Start your application:

pm2 start server.js --name my-node-app

Check running applications:

pm2 list

View application logs:

pm2 logs my-node-app

Restart the application:

pm2 restart my-node-app

Stop the application:

pm2 stop my-node-app

Save the current process list:

pm2 save

To configure startup after a server reboot:

pm2 startup

Follow the command printed by PM2, then run pm2 save again.

PM2 helps manage the application process, but it does not replace server security, monitoring or backups.

✦ 8. Configure Domain and HTTPS

After deploying your application, you can connect a domain name.

For example:

www.example.com

A typical setup includes:

  1. Purchase a domain name.

  2. Point its DNS records to your server or hosting provider.

  3. Configure a reverse proxy such as Nginx if you are using a VPS.

  4. Forward incoming requests to your Node.js application.

  5. Configure HTTPS using a valid TLS certificate.

Example Nginx configuration for a Node.js application running on port 3000:

server {
    listen 80;
    server_name example.com www.example.com;

    location / {
        proxy_pass http://127.0.0.1:3000;

        proxy_http_version 1.1;

        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;
    }
}

After testing and enabling the configuration, use a trusted TLS certificate to enable HTTPS.

Why is HTTPS important?

  • Encrypts communication between users and your server.

  • Protects login and form data during transmission.

  • Helps establish trust with website visitors.

  • Is important for secure production applications.

✦ 9. Production Security Best Practices

Security should be considered before making an application publicly available.

Follow these practices:

1. Protect Sensitive Information

Never expose database passwords, API keys or JWT secrets in frontend code.

2. Use HTTPS

Secure communication using a valid TLS certificate.

3. Validate User Input

Validate data received through forms, query parameters and API requests.

4. Keep Dependencies Updated

Check for known dependency vulnerabilities:

npm audit

Review the reported issues and apply suitable updates.

5. Configure CORS Properly

Allow only trusted origins when your frontend and backend are hosted separately.

6. Use Secure Authentication

Hash passwords using a suitable password-hashing library. Never store plain-text passwords.

7. Configure Firewall Rules

On a VPS, expose only the required public ports. Keep internal application ports private when using a reverse proxy.

✦ 10. Logging and Monitoring

After deployment, you should monitor your application regularly.

Useful commands:

pm2 status
pm2 logs
pm2 monit

You should monitor:

  • Application errors

  • CPU and memory usage

  • Server availability

  • Database connection problems

  • Response time

  • Failed login attempts

A health endpoint such as /health can help monitoring systems check whether the application is responding.

✦ 11. Graceful Shutdown in Node.js

A production application should close its resources properly when the process receives a shutdown signal.

Example:

const express = require('express');

const app = express();

const PORT = process.env.PORT || 3000;

const server = app.listen(PORT, () => {
    console.log(`Server running on port ${PORT}`);
});

process.on('SIGTERM', () => {
    console.log('SIGTERM received. Closing server...');

    server.close(() => {
        console.log('HTTP server closed.');
        process.exit(0);
    });
});

In a real application, close database connections and other resources before exiting.

✦ 12. Common Node.js Deployment Problems

Problem Possible Solution
Application not starting Check server logs and startup command
Port already in use Check the port configuration
Database connection failed Verify database credentials and network access
Module not found Install dependencies using npm
Environment variable missing Check production environment settings
Website not opening Verify DNS, firewall and reverse proxy
HTTPS not working Check TLS certificate and server configuration

✦ Conclusion

Deploying a Node.js application is an important skill for every backend developer. By learning hosting, environment variables, PM2, domain configuration, HTTPS and production security, you can make your applications accessible to real users.

Start with a small Node.js project, deploy it on a test server and gradually learn production management.

At Param Classes, students can learn practical Web Development and backend technologies through hands-on projects. For website development and digital solutions, visit ParamWebInfo.

✦ Frequently Asked Questions (FAQs)

Q1. What is Node.js deployment?

Node.js deployment is the process of hosting a Node.js application on a server so users can access it over the internet.

Q2. Which hosting is best for Node.js?

VPS, cloud infrastructure and managed application platforms are common options. The best choice depends on your budget and application requirements.

Q3. What is PM2 in Node.js?

PM2 is a process manager that helps run, monitor and restart Node.js applications.

Q4. Why do we use environment variables?

Environment variables store configuration and sensitive values separately from application source code.

Q5. Can Node.js run on Linux hosting?

Yes. Node.js applications can run on Linux servers when the required Node.js runtime and dependencies are installed.

Q6. Why is HTTPS required?

HTTPS encrypts communication between the browser and server and helps protect sensitive information.

Q7. Can I deploy a Node.js application for free?

Some hosting platforms offer free or trial options, but availability, usage limits and pricing can change. Always check the current hosting plan before deployment.

←
Post 38 of 40
→