Special Sale
StableCluster Logo

Quick Guide: Deploying a Node.js, Express, and Mongoose REST API on cPanel

Step-by-step guide to deploying Node.js, Express, MongoDB REST API on cPanel. With easy setup and troubleshooting tips.
Jan 2, 2025Saurav Bista3 minutes read

So, you are a Node.js developer trying to deploy your Express and Mongoose REST API on cPanel. Well, you’ve come across the perfect guide that guides you through each step. Whether you’re new to development or are just looking for a smooth process, this guide is set to take you through a smooth process for a hassle-free deployment. 

Following this guideline, you’ll master the framework of Node.js, i.e., Express deployment on cPanel, an essential skill for web developers. This step-by-step guide covers each tiny detail and helps you successfully deploy your REST API. So, let’s get started and learn to deploy your Node.js, Express, and Mongoose API with MongoDB!

Step-by-Step Deployment Process 

  1. Prepare Your Backend Project:
    • Make sure, you have your Node.js, Express, and Mongoose application ready for deployment.
    • Check your package.json to see if it contains all dependencies and scripts (e.g., “start”: “node server.js”).
  2. Zip Your Project:
    • Zip your project folder.
    • Don’t include the node_modeules directory while zipping your project ( use .gitignore or manually exclude it).node_modules
    • Create a .env file that stores your environment variables like DB connection strings and API keys.
  3. Login to cPanel:
    • Log into your cPanel account.

cpanel-login

  1. Setup Node.js Application:
    • In cPanel, navigate to the Software section and click Setup Node.js App.

node.js

    • Choose the appropriate Node.js version (same as your app version), the Application Mode (choose “development”), the Application Root (the folder where you’ll upload your app), and the Application URL (public URL through which your app will be accessed)

application root

    • Click Create to set up the app.
  1. Add Environment Variables:
    • After creating the Node.js application, you’ll need to add any necessary environment variables (MONGO_URI and PORT) inside your Node.js App section.

env-variable

    • Alternatively, you can also create a .env file in your project’s root directory and add the required variables.
  1. Upload Your Project Files:
    • Navigate to File Manager in cPanel, where you’ll see the Application Root folder.
    • Inside the Application Root folder, upload your zipped project file.

project

    • After uploading, extract the zip file inside the Application Root folder.

extraction

  1. Activate Virtual Environment:
    • Inside the Setup Node.js App, you’ll find a code snippet to activate the environment. This typically looks like this:

activation

    • Copy this code and paste it into the Terminal for activation.
  1. Install Dependencies:
    • Open the Terminal in cPanel and move to your project folder ( where you extracted your project)
    • Run: npm install in the Terminal

npm install

    • This will install all the required dependencies mentioned in your package.json.
  1. Start the Application:
    • Once all the dependencies are installed, return to the Setup Node.js App section and start the app.
    • You can also start the app manually using the Terminal by running npm start
  2. Access Your Application:
    • Click on the URL to start your app and see your project load. 
    • In some cases, after installation, problems like this can arise:

    • Then, you can add a route in your hostname “/API/tasks.”
    • Check connection of MongoDB ( review the logs and run test query).

Troubleshooting Common Issues

As you navigate the deployment process, you will encounter various challenges. Let’s look at some common issues you might face, alongside tips to address them:

  1. Missing Dependencies: One of the most common errors faced during deployment. Most missing dependencies related errors can be fixed by simply running  npm install  (This ensures that all the necessary packages are installed)
  2. Environment Variables: If your app fails to connect with MongoDB, double-check your .env file for the correct variables.
  3. Permissions: Ensure that the app’s files and directories have the correct permissions to run on the server.
  4. Logs: Check for Error Logs in your cPanel if your app crashes or fails to start. You can figure out where the problem lies by just checking the log.

Conclusion

While deploying your Node.js, Express, and Mongoose REST API with MongoDB on cPanel might seem challenging, this beginner-friendly guide displays the easiest method for a solid deployment. This guide was made to get your app running and help people understand the key components of deployment. With this guide, you get concepts of key terms like environment variables, application configuration, and dependency management.

Good luck with your deployment!!

 

50% OFF

🌟 It’s the last chance to grab an exciting offer from StableCluster.

with the discount code

NEWYEAR2025

Share This Article


Copy to clipboard
Saurav Bista
Saurav Bista

Saurav is a seasoned content writer at StableCluster with a strong foundation in Information Systems and over four years of experience in creating impactful, insightful content. Combining his passion for writing and technology, he excels at crafting narratives that deeply connect with readers, inspiring confidence and guiding them through the complexities of the digital landscape.