Prerequisites
- Node.js - Installed
- MongoDB Atlas - AWS free tier cluster Account
- Text-editor - Visual Studio Code, Atom and etc.
- Git - Installed
Dependencies Used
- cors version 2.8.5
- express version 4.17.1
- mongoose version 5.7.12
Assumptions
The RESTful Node.js server with MongoDB database works when running on your localhost.
The MongoDB Connection String, you can find it by:
- Sign-in at mongoDB Altas
- Locate the cluster you're using for server
- Click " CONNECT" > Click " Connect Your Application
Set MongoDB Altas - whitelist to 0.0.0.0/0
To set click 'Network Access' under Security tab > click 'ADD IP ADDRESS'
note
Whitelist - MongoDB Atlas allows client connections to the cluster from entries in the project’s whitelist. You must add an entry to the whitelist to connect the client.CIDR notation 0.0.0.0/0 is used to allow all client connections to the cluster. However, you can add a specific IP address/es.
- Go to Heroku dashboard > Select your server App > Settings > Reveal Config Vars
Add ' MONGODB_URL ' as a Config Vars KEY in HEROKU, and add connection string as the VALUE then click 'Add'.
Store Cluster - connection string as a node.js process environment variable called ' MONGODB_URL ' in the main server javascript file.
For Example: server.js
const db = process.env.MONGODB_URL;
const connectDB = async () => {
try {
await mongoose.connect(db, {
useUnifiedTopology: true,
useNewUrlParser: true
});
console.log("MongoDB is Connected...");
} catch (err) {
console.error(err.message);
process.exit(1);
}
};
To allow inbound requests to the server using CORS
- Add FrontEnd site URL where it says "Your FrontEnd Website URL"
For Example:
// cors origin URL - Allow inbound traffic from origin
corsOptions = {
origin: "Your FrontEnd Website URL",
optionsSuccessStatus: 200 // some legacy browsers (IE11, various SmartTVs) choke on 204
};
app.use(cors(corsOptions));
note
The code example above is for allowing a single-origin if you want to allow more than one origin refer here
To Deploy select your App then click "Deploy" and choose one of the deployment methods.
Troubleshooting Tips
- If you encounter Heroku runtime errors you can view logs below
note
- If you are getting errors stating Heroku can't find your modules when trying to deploy, it may be because you are using relative paths for example:
const connectDB = require("./config/db");
You will have to use absolute paths because Heroku creates its own FileSystem at deployment.
Top comments (1)
Thanks for sharing the amazing information with us. If you want to host a restful Node.js server with MongoDB then It’s very much useful for your business.
I also know that one of the hosting companies for Node.js server with MongoDB. The name of the best hosting provider is DomainRacer.
DomainRacer is the best and most powerful web hosting company that provides a Node.js server with MongoDB platform.
Learn more Information - dedicatedcore.com/best-node-js-vps...