How to Connect Node.js Application with MongoDB on CentOS

How to Connect Node.js Application with MongoDB on CentOS

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.

Prerequsities

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.

npm install mongoose

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://127.0.0.1:27017/test');

// 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.

node test_server.js

Output – database connected successfully

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

Thankyou.


    • Related Articles

    • How-to-set-up-a-node-js-application with apache on-centos-7

      Node.js is an open source Javascript programming environment to make server-side and networking applications simple to create. Installing Node.js First of all, You need to enable node.js yum repository in your system provided by the Node.js official ...
    • How To create and connect an FTP Account in cPanel

      Step 1 : Login into you Cpanel by opening “ server-ip:2087 ” in the URL of the browser , Step 2 : login into the WHM using root credentials . Step 3: Open List Accounts . Step 4 : Open the account of cpanel by clicking on the Cpanel Icon . Step 5 : ...
    • Use NGINX as a Reverse Proxy

      What is a Reverse Proxy? A reverse proxy is a server between internal and external clients which transmits clients requests to a different server. Although other standard applications, such as Node.js, can support themselves, NGINX has a range of ...
    • Nginx Installation in CentOS 7

      Step 1: Add Nginx repository . Command :  # yum install epel-release Step 2:  Install Nginx using following command . Command: # yum install nginx Step 3: Start the service of Nginx . Command :  # systemctl start nginx Step 4: Check status of Ngnix . ...
    • Apache Virtual Hosts setup on CentOS 7

      Introduction The web server of Apache is the most popular way to deliver web content. It serves over half of all active websites in the Internet and is extremely powerful and flexible. Apache divides its features and components into separate units, ...