Did You Know?

How Can We Help?

Special Offer

Deploy your Cloud Server now and get $100 in Free Credits!

Get Started

How to Connect Node.js Application with MongoDB on CentOS

Table of Content

This article will help you to connect Node.js application with MongoDB. Also, configure MongoDB drive for nodejs using Mongoose node application on CentOS and Redhat systems.


We assume that Node.js and MongoDB are already installed on your server. If not installed first follow our guide below to complete the installation.

Install mongoose Module

Mongoose offers a simple schema-based approach for modeling data about the program which includes built-in typecasting, validation and much more.

[console]npm install mongoose[/console]

Connect Nodejs with MongoDB

Create a file test server.js, and add content to the file below. For the more details about working with Node.js and MongoDB with mongoose read this tutorial.

</ Sample script of Node.js with MongoDB Connection

// This code requires mongoose node module
var mongoose = require('mongoose');

// Connecting local mongodb database named test
var db = mongoose.connect('mongodb://');

// testing connectivity
mongoose.connection.once('connected', function() {
 console.log("Database connected successfully")

Now we execute the test_server.js using node. If we get message “Database connected successfully”, It means our node.js app is successfully connecting database.

[console]node test_server.js[/console]

Output - database connected successfully

We have connected Node.js Application with MongoDB successfully on CentOS.


Special Offer

Deploy your Cloud Server now and get $100 in Free Credits!

Get Started
Table of Contents