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!
- 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”).
- Zip Your Project:
- Zip your project folder.
- Don’t include the node_modeules directory while zipping your project ( use .gitignore or manually exclude it).
- Create a .env file that stores your environment variables like DB connection strings and API keys.
- Login to cPanel:
- Log into your cPanel account.
- Setup Node.js Application:
- In cPanel, navigate to the Software section and click Setup Node.js App.
-
- 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)
-
- Click Create to set up the app.
- 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.
-
- Alternatively, you can also create a .env file in your project’s root directory and add the required variables.
- 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.
-
- After uploading, extract the zip file inside the Application Root folder.
- Activate Virtual Environment:
- Inside the Setup Node.js App, you’ll find a code snippet to activate the environment. This typically looks like this:
-
- Copy this code and paste it into the Terminal for activation.
- Install Dependencies:
- Open the Terminal in cPanel and move to your project folder ( where you extracted your project)
- Run:
npm install
in the Terminal
-
- This will install all the required dependencies mentioned in your package.json.
- 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
- 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).
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:
- 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)
- Environment Variables: If your app fails to connect with MongoDB, double-check your .env file for the correct variables.
- Permissions: Ensure that the app’s files and directories have the correct permissions to run on the server.
- 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.
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!!