Wednesday, November 12, 2014

NodeJs web service With MongoDb

NodeJs is one of the powerful Lagrange that can do lot of things in programming world. When size of data become larger most of databases not can handle them efficiently. Therefor it leads to the change of database structure. Relational (SQL) database is not the best solution in most cases. NoSQL (Not Only SQL) databases are the best solution in those situations. One of the best NoSQL databse is MongoDb.

You can follow above hyperlinks to download and configure Nodejs and MongoDb within your local machine. if you having trouble  just comment below..ill update another post about configurations.

With this post I am going to demonstrate how to create REST service with NodeJs and MongoDB

You can use SublimeText as text editor for node js

1 . First create Document in MongoDb Name it and UserDetails

2. Add following Data to that document
/* 0 */
{
    "_id" : ObjectId("54608e50feeb4595a4f09651"),
    "userName" : "Harith",
    "Age" : 25,
    "Expired" : true
}
 
/* 1 */
{
    "_id" : ObjectId("54608ea4feeb4595a4f09652"),
    "userName" : "Jaliya",
    "password" : "123456",
    "Age" : 30
}
 
/* 2 */
{
    "_id" : ObjectId("5461d46dfeeb4595a4f09655"),
    "name" : "Joe Bookreader",
    "address" : {
        "street" : "123 Fake Street",
        "city" : "Faketon",
        "state" : "MA",
        "zip" : "12345"
    }
}
 
/* 3 */
{
    "_id" : ObjectId("54608f20feeb4595a4f09653"),
    "UserId" : "1",
    "address" : {
        "street" : "123 Fake Street",
        "city" : "Faketon",
        "state" : "MA",
        "zip" : "12345"
    }
}


Lets start to create NodeJs REST

First create Javascript file named it as Rest.js

To use MongoDb you need to install flowing npm package by this command
 npm install mongojs

Then add package.json file to the root of the folder and copy this to it

{
  "name""Rest-server",
  "version""0.0.1",
  "private"true,
  "dependencies": {
    "express""3.3.4"
  }
}

Go to CMD and type npm install




Then start to coding real service in Rest,js

1. Import following libraries to the js file
var express = require('express');
var mongojs = require('mongojs');


2. Then create Array to store all data that comes from Mongo Database
var data = [];

3. Lets code REST Like this
// custom package
var app = express();
 
// Array definition to store data
var data = [];
 
// Http Normal get
app.get('/'function (req, res) {
    var db = require('mongojs').connect('mongodb://localhost:27017/DocumentOrDb');
    console.log("Server is in listen1");
    db.collection("UserDetails").find(function (err, docs) {
        data = [];
        docs.forEach(function (item) {
            data.push(item);
        })
    });
 
    console.log("Server end");
    res.send(data);
});

then run the service with cmd node Rest.js


Here is the full code


/*
  Rest Api with MongoDb Database 
  Author : prabathsl
  Copyright © prabathsl 2014
*/
 
// Libraries
var express = require('express');
var mongojs = require('mongojs');
 
// custom package
var app = express();
 
// Array definition to store data
var data = [];
 
// Http Normal get
app.get('/'function (req, res) {
    var db = require('mongojs').connect('mongodb://localhost:27017/DocumentOrDb');
    console.log("Server is in listen1");
    db.collection("SampleBigdata").find(function (err, docs) {
        data = [];
        docs.forEach(function (item) {
            data.push(item);
        })
    });
 
    console.log("Server end");
    res.send(data);
});


 Enjoy :) 



0 comments: