How to Connect MongoDB with Your Node.js App in cPanel

How to Connect MongoDB with Your Node.js App in cPanel

How to Connect MongoDB with Your Node.js App in cPanel

How to Connect MongoDB with Your Node.js App in cPanel

Step-by-step guide for integrating MongoDB into your Node.js project on Hiverift or any cPanel hosting environment.

Overview

Whether you’re using MongoDB Atlas (cloud-based) or a local MongoDB server, this guide will help you integrate it into your Node.js app hosted via cPanel.

Requirements

  • Node.js App hosted in cPanel (via Application Manager)
  • MongoDB Atlas account or access to MongoDB URI
  • mongoose or MongoDB native driver installed

1. Install Mongoose in Your Node.js App

  1. Login to your cPanel
  2. Go to Setup Node.js App
  3. Open Terminal in the app root directory or use SSH
  4. Run the following command:npm install mongoose

2. Get Your MongoDB URI

  • For MongoDB Atlas:mongodb+srv://username:password@cluster.mongodb.net/dbname?retryWrites=true&w=majority
  • For local/hosted MongoDB:mongodb://localhost:27017/mydatabase

3. Add Your MongoDB URI as an Environment Variable (Recommended)

  1. In cPanel, go to Setup Node.js App
  2. Click Edit on your application
  3. Scroll down to Environment Variables
  4. Add:MONGODB_URI = your-mongodb-uri
  5. Click Save and then Restart the app

4. Update Your Node.js Code

In your main file (e.g. index.js), use the following code to connect MongoDB:

const mongoose = require('mongoose');

const uri = process.env.MONGODB_URI;

mongoose.connect(uri, {
  useNewUrlParser: true,
  useUnifiedTopology: true
})
.then(() => console.log('MongoDB connected successfully'))
.catch(err => console.error('MongoDB connection error:', err));

Alternative: Use MongoDB Native Driver

const { MongoClient } = require('mongodb');
const uri = process.env.MONGODB_URI;

const client = new MongoClient(uri);
async function run() {
  try {
    await client.connect();
    console.log("Connected to MongoDB");
    const db = client.db("your-database");
    // perform actions on the collection
  } finally {
    await client.close();
  }
}
run().catch(console.dir);
Tip: Never hardcode credentials. Always use environment variables for security and portability.

Troubleshooting

  • Check cPanel Error Logs if your app fails to start
  • Ensure your IP is whitelisted in MongoDB Atlas (under Network Access)
  • Confirm mongoose or mongodb is listed in package.json

Need Help?

Contact our Hiverift support team at dev@hiverift.com for MongoDB setup assistance with your Node.js app.

© 2025 Hiverift. All rights reserved.

 

Post Your Comment

Build Your Website with HiveRift

From professional business to enterprise, we’ve got you covered!

©2025, Hosting. All Rights Reserved by KhatuShyam Technologies

HiveRift
Privacy Overview

This website uses cookies so that we can provide you with the best user experience possible. Cookie information is stored in your browser and performs functions such as recognising you when you return to our website and helping our team to understand which sections of the website you find most interesting and useful.