Showing posts with label nodejs mongodb free hosting. Show all posts
Showing posts with label nodejs mongodb free hosting. Show all posts

Thursday, 6 November 2025

Connecting Your Node.js API to MongoDB on Free Hosting

 


You've got a Node.js API and a MongoDB database, both living comfortably on your free cPanel hosting. But how do you make them talk to each other? This is the most crucial step in getting your full-stack application to work.

This tutorial will guide you through connecting your Node.js application to the free MongoDB database provided in your cPanel account.

Prerequisites:

  • A Node.js app deployed on your free cPanel hosting.

  • A MongoDB database created in cPanel (see our main guide for how to do this).

  • Basic knowledge of using Mongoose in Node.js.


Step 1: Create Your MongoDB Database & User in cPanel

  1. Log into cPanel.

  2. Find and click the "MongoDB® Databases" icon.

  3. Under "Create New Database," enter a name (e.g., myappdb) and click "Create Database."

  4. Scroll down to "Add MongoDB User." Create a username and a strong password. Click "Create User."

  5. Finally, in the "Add User To Database" section, assign the user you just created to the database. Grant all privileges.

Step 2: Get Your Connection String

The connection details are all within cPanel. Your connection string will be structured as follows:

mongodb://USERNAME:PASSWORD@localhost:27017/DATABASE_NAME

  • USERNAME: The user you created (e.g., dev_user).

  • PASSWORD: The strong password you set.

  • DATABASE_NAME: The full name of your database, which is usually cpanelusername_dbname (e.g., mycpanel_myappdb).

You can find the exact connection string back in the "MongoDB® Databases" section after creation.

Step 3: Configure Your Node.js Application

In your local Node.js project, you'll need to set up the connection. We'll use the popular Mongoose ODM.

  1. Install Mongoose:

    bash
npm install mongoose

Create a Database Connection File: Create a file like db/connect.js.

javascript
// db/connect.js
const mongoose = require('mongoose');

const connectDB = async () => {
    try {
        // Use the connection string from Step 2
        const conn = await mongoose.connect('mongodb://dev_user:your_strong_password@localhost:27017/mycpanel_myappdb');
        console.log(`MongoDB Connected: ${conn.connection.host}`);
    } catch (error) {
        console.error(error.message);
        process.exit(1); // Exit the process with failure
    }
};

module.exports = connectDB;

Step 4: Deploy and Test the Connection

  1. Upload Your Code: Using File Manager or Git, upload your entire Node.js project to your cPanel account, including the updated code with the Mongoose connection.

  2. Install Dependencies: In your cPanel's "Node.js Manager", select your app and click "Run NPM Install."

  3. Restart Your Application: After NPM install finishes, click "Restart" on your Node.js application.

Step 5: Check the Logs for Success

In the "Node.js Manager", check the application logs. If the connection was successful, you should see a message like:
MongoDB Connected: localhost

If you see an error, double-check:

  • Your connection string for typos.

  • That your MongoDB username and password are correct.

  • That the user has the correct privileges on the database.

Congratulations! Your Node.js API is now successfully connected to your MongoDB database, all running on your free cPanel hosting. You can now build and manage your full-stack application with a fully functional backend.

Stop worrying about costly cloud bills for development. Your free, fully-connected backend environment is ready.
>> Build Your App with Free Node.js & MongoDB