Get your server issues fixed by our experts for a price starting at just 25 USD/Hour. Click here to register and open a ticket with us now!

Author Topic: Install Node.js and MongoDB on CentOS  (Read 3761 times)

0 Members and 1 Guest are viewing this topic.

akhilt

  • Guest
Install Node.js and MongoDB on CentOS
« on: July 21, 2018, 04:17:14 pm »
Install Node.js and MongoDB on CentOS

Node.js is a platform built on Chrome’s JavaScript runtime for easily building fast, scalable network applications.

Please follow the below steps to install node.js:

Step 1 – Add Node.js Yum Repository

First of all, You need to enable node.js yum repository in your system provided by the Node.js official website. You also need development tools to build native add-ons to be installed on your system.

For Latest Release:-

Code: [Select]
# yum install -y gcc-c++ make
# curl -sL https://rpm.nodesource.com/setup_10.x | sudo -E bash -

For Stable Release:-

Code: [Select]
# yum install -y gcc-c++ make
# curl -sL https://rpm.nodesource.com/setup_8.x | sudo -E bash -

Step 2 – Install Node.js and NPM

After adding yum repository in your system, install Node.js package. NPM will also be installed with node.js. This command will also install many other dependent packages on your system.:

Code: [Select]
# yum install nodejs
Check Node.js and NPM Version using:

Code: [Select]
# node -v
# npm -v

MongoDB is a fully flexible index support and rich queries database.

Please follow the below steps to install MongoDB:

Step 1 – Add MongoDB Yum Repository

Add the following content in yum repository configuration file mongodb.repo as per your required MongoDB version and system architecture. For this article, we are using MongoDB 4.0 repository.

Code: [Select]
# vi /etc/yum.repos.d/mongodb.repo

[MongoDB]
name=MongoDB Repository
baseurl=http://repo.mongodb.org/yum/redhat/$releasever/mongodb-org/4.0/x86_64/
gpgcheck=0
enabled=1

Step 2 – Install MongoDB Server

Code: [Select]
# yum install mongodb-org
Step 3 – Start MongoDB Service

Code: [Select]
# /etc/init.d/mongod restart
Use the following command to check installed MongoDB version:

Code: [Select]
mongod --version
You can connect Node.js application with MongoDB using the Mongoose node application on CentOS. Mongoose provides a straightforward schema-based solution to modeling your application data and includes built-in typecasting, validation and many more. To install mongoose Module:

Code: [Select]
# npm install mongoose
To connect Nodejs with MongoDB:

Create a test.js file and add below content to the file:

// 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")
});

Hope this is helpful!!