Sunday, June 17, 2018

Express JS



Express JS is flexible NODE.JS web application framework that provides a robust set of features to develop mobile and web applications. 


It also facilitates the rapid development of Node based Web Applications.


Features of Express JS

  • Allows setting up of middle-ware to respond to HTTP Requests.
  • Defines a routing table which is used to perform different actions based on HTTP Method and URL



Pre-Requisites

1. Node JS
2. MS SQL Database (for demo - any database can be used)


Step-1


Step 1: Create a package.json file.
{
  "name": "<Application Name>",//e.g.  – node_app
  "version": "0.0.0",
  "description": "<Description about your application>",
  "main": "server.js",
    "dependencies": {
        "express": "4.14.0",
        "body-parser": "1.15.2",
         "mssql":"3.3.0"
    },
    "scripts": {
    "start": "node server.js"
  },
  "author": {
    "name": "<Your Name>",
    "email": "<Your Email>"
  }
}
Step-2

Step 2: Create server.js file.


//Initiallising node modules
var express = require("express");
var bodyParser = require("body-parser");
var sql = require("mssql");
var app = express();

// Setting Base directory
app.use(bodyParser.json());

//CORS Middleware
app.use(function (req, res, next) {
//Enabling CORS
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Methods", "GET,HEAD,OPTIONS,POST,PUT");
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, contentType,Content-Type, Accept, Authorization");
next();
});

//Setting up server
var server = app.listen(process.env.PORT || 8080, function () {
var port = server.address().port;
console.log("App now running on port", port);
});

//Initiallising connection string
var dbConfig = {
user: "<username>",
password: "<password>",
server: "<severName>",
    database: "<database",
    port: "<port>",
    requestTimeout: '10000',
    debug: true,
    dialect: "mssql",
    options: {
encrypt: true // Use this if you're on Windows Azure
}
};

var pool = function(){
var conn = new sql.Connection(dbConfig, function(err){
var request = new sql.Request(conn);
//console.dir(request);
return request;
});
return conn;
}

//Function to connect to database and execute query
var executeQuery = function(res, query){
    var dbConn = new sql.Connection(dbConfig);
    dbConn.connect().then(function () {
        var request = new sql.Request(dbConn);
        request.query(query).then(function (recordSet) {
            //console.log(recordSet);
            res.send(recordSet);
            dbConn.close();
        }).catch(function (err) {
            console.log(err);
            dbConn.close();
        });
    }).catch(function (err) {
        console.log(err);
    });
    
}

app.get("/api/dpc", function(req , res){
    var query = "select count(*) as count from [schema].[table]";
    executeQuery(res,query);
});
   

Step -3


npm install 


Step-4

node server.js

The node server will start running into port number 8080 with corresponding API Routes; you can point to the below URL to test the application.



Node JS

Node.JS is powerful JavaScript framework built on top of Google Chrome's JS V8 Engine. 

It is used to develop I/O intensive web applications like video streaming sites, single-page applications, etc.. Node.JS is open source and used by thousands of developers around the world.


Advantages

  1. Quick & easy development
  2. High performance
  3. Run on single thread to handle multiple concurrent requests
  4. Easy to write API and interaction code
  5. Streaming support
  6. Monitoring possibilities
  7. Authentication support
  8. Lightweight, fast, and scalable

Saturday, June 16, 2018

Micro Service Introduction

A microservice is a single self-contained unit which, together with many others, makes up a large application. By splitting app into small units every part of it is independently deployable and scalable, can be written by different teams and in different programming languages and can be tested individually.






PROs of Microservices architecture:
  • The application takes lesser time to start, which increases developers productivity and speeds up deployments.
  • The system can be scaled easily and boost system performance.
  • Each service can be deployed without affecting other services, which allows deployment of new versions of services frequently.
  • Microservices are independent of each other and have a specific role. Changes made to one microservice does not affect other.
  • No restriction of technology in developing any service, there by making system technologically independent.
CONs of Microservices:
  • Deployment complexity increases as the need to manage a system comprised of many different service types.
  • The developer needs to handle complexity for creating separate distributed system.

Microservices versus monolithic architecture when application grows

Monolithic
  • Understanding huge code base becomes difficult, especially for new developers.
  • Large code files make development environment slow like IDE.
  • Changing technology or language becomes complicated as the code base is tightly coupled.
  • CI/CD becomes challenging and time-consuming and needs a dedicated team to manage it.
Microservices
  • Microservices are small and specific to business requirements.
  • Microservices are loosely coupled and can be easily developed and deployed.
  • Development can be done using different technologies.
  • Microservices can be built with new technology stack enabling faster development cycle.
  • Microservices can be easily scaled on demand.

Why Microservices with Node.JS
  • Productivity – Node.js uses NPM (node package manager) which comes up with tons of ready to use modules which speed up the development process. Node.js uses javascript as interface language so that same language can be used for back end and front end. This saves lots of development time.
  • Performance – Node.js event driven I/O model along with microservices strategy which can handle an extreme amount of load with lesser response time.
  • Developer Friendly – Node.js has vast community support and is backed by millions of developers across the globe. Node package manager (NPM) contains more than 4 million ready to use modules which serve as heaven for developers. Node.js enables faster development of applications which are easily scaleable, there by making the developer happy through out application development life cycle.
Node.js supports multiple Microservices framework which enables development of application based on microservices strategy. Most popular among them are Senaca js.  Senaca js provides a complete environment for the development of the microservice based application.

x