Saturday, December 06, 2014

How to Scale Your Node.js App - Cluster API & Express with Benchmarking Siege

Though Many Programming language emerge,Javascript Becomes more Powerful Day by Day competing from Front End to back End server and Entering into Desktop apps and Mobile Apps.And becoming popular over GitHub and trending over the world.I have been looking for scaling my Node.js App instance.However it bacame famous by one of it's reason SINGLE THREADED and ASYNCHRONOUS I/O operations.So, now lets create some master and Worker for our Node.js Instance make it run for us in available Processors in Server/Cloud to Balance our Load and Scale Your Node.js Application even Faster.Our Ultimate Aim is to use the Multiple Processor for our App and Maintain a Stable state between Master and Slave Workers and Communication between them.

How to Scale Your Node.js App - Cluster API & Express with Benchmarking Siege
How to Scale Your Node.js App - Cluster API & Express with Benchmarking Siege

Reference : Download 

Prerequisites :

I would like to go through my previous articles on Basics of Node.js with Express Framework.
Cluster API is Inbuilt Feature of Node.js ,We are just Going to test in our app and use it out.However use it with caution since it is marked as Experimental Feature till Right now.

How Does it Works ? 


Here we are creating a new Worker from master Cluster and working with the Workers,Generally you can fork process based on availability of processor.And if you like to fork even more,just Go through this Stackoverflow Thread.So,your Workers will handle your works automatically to load your balance.

Procedure :


we check it is master process or not and forking process as worker as do our specified operation to respond the client.




 var cluster = require("cluster");

 var cpu = require('os').cpus().length;

 if (cluster.isMaster) {
  
  console.log(cpu);
    cluster.fork();
    cluster.fork();

  
 cluster.on('online', function(worker) {
   console.log("Worker Started pid : "+ worker.process.pid);
 });

 cluster.on('exit', function(worker, code, signal) {

     console.log('worker ' + worker.process.pid + ' stopped');
   });

}
 

Worker Process :

And with else you can write your application logic here to run with worker and respond to client.here you can notify the master thread with IPC and cmd.however we will do some basic operation over it.


 else {
  var express = require("express");
  var app = express();

 app.get("/",function(req,res){

  console.log("request : "+cluster.worker.process.pid); 
  res.send("Welcome to Node.js Cluster API Demo from"+ cluster.worker.process.pid);
 });

 http.createServer(app).listen(3000);


 }



Test with Siege :

Now lets test with Siege tool.Download it from here appropriate operating system install it or extract and use.


use the command as below with your own options to check out the efficiency for our small app.



C:\siege-windows>siege -t1M -c100 -d10s http://localhost:3000

** SIEGE 3.0.5
** Preparing 1500 concurrent users for battle.


With Cluster API node.js 

Without Cluster/Normal Node.js Single Threaded







Further Functions:

You have lot of functions to do with worker and Events to do with Worker and Master and inter process communication is also possible to do Cluster module.Search through npm for more ready made cluster modules for your apt application in production.Check here below for reference guide

Note :

The main thing is to scale the application both Horizontally and vertically.this can be achieved not only by clusters,you can run it in seperate VM's for more scalling.I am too looking for such experience and article.

Hope you Learnt Something quite useful.For Errors/Bugs/hugs/suggestion comment below or mail to s.shivasurya@gmail.com or connect with me in Facebook/Twitter/Linkedin/G+ hangouts.share is care.


Saturday, November 08, 2014

jQuery Animated Number Counter From Zero To Value - Javascript Animation

JQuery is powerful Library of JavaScript , Which makes the Web Development more Faster comparing plain JavaScript and completely useful for Beginners for building such application in minutes with lot of Functions in single line.And most powerful feature is JQuery Ajax which makes the users to relaxful usage of web app without page reloading and easy for developers to work with callbacks.Jquery is also famous for it's Animation and making app more amazing and pleasing with speed, fade etc..
There are also lot of library which are built upon JQuery as Library,such as Bootstrap and so on.




Install : Demo & Download


You must need Jquery.js library use just cdn from google.


<script src="//ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>


Jquery:

Using animate function JQuery we could perform this animation,here is the actual Documentation

  • first argument is CSS property ,which we would generally used to animate.
  • second argumet as option
option : 

  1. duration  : amount of time in milliseconds for the animation occurance
  2. easing  : easing function for transition 
  3. step : This function provides an opportunity to modify the Tween object to change the value of the property before it is set.

Logic css with Javascript :

We get the count span counter CSS property starting from zero within a for each loop in jquery and setting the Counter as text by CSS property inside animation function and next comes option which has SWING as transition and then with duration and finally comes step option which is used to once again change the value before set and rounded off with math.ceil function.

Code and Demo :

Demo might load slow.have a look at demo hosted +codepen.ReRun the Code to see the counter changes!

$('.Count').each(function () {
    $(this).prop('Counter',0).animate({
        Counter: $(this).text()
    }, {
        duration: 4000,
        easing: 'swing',
        step: function (now) {
            $(this).text(Math.ceil(now));
        }
    });
});
See the Pen FatiB by s.shivasurya (@shivasurya) on CodePen.


Hope You have Enjoyed this post.Since it was cool one actually showing up in most of the modern websites , which made me to search and compile to perform this animated counter.and a reader too requested via me in Facebook to do with JQuery.

For Errors/Comments/Bugs/Hugs mail me s.shivasurya@gmail.com or connect with me in Facebook/Twitter you can raise your exceptions/messages!
share is care.

Wednesday, October 29, 2014

Passport Authentication For Local Strategy - Node.js With Express Framework & Mongoose Database

Registration/Login is famous part in Web Based Application to enable User Privacy and track their records and Changes made.This is Easy in PHP -where i have written already Three post regarding login system for web Application.However in node.js it is quite easy for us to implement in faster manner.Passport.js comes here to help us in different strategy for implementing authentication system and integrate with Database and Session.

Passport.js module is well matured module which is used to Authenticate existing user and create new user for corresponding Strategy.here Strategy is nothing but simply just a way of authentication using username/password.



Reference : Download |  Demo 

Installation :

Before starting this module, i would like to go through some of My Last post on basic articles in node.js
C:\> npm install passport -g

This will install passport module globally in your local machine.however there are lot of strategy.we are going to use local-strategy.

App structure :

App structure is general where we would have a ,

1) models [directory]  - contains models => models.js(structure of Database) 
2) config[directory] - contains database connection url and passport.js Logic and functions.
3) view[directory] - our Front end to parse messages and display written with Templating Language (we have used EJS template)

here we are going to use express.js for our convenience to manage routes.

passport.js

Lets start with Passport.js logic and Working.Generally this is a strategy authenticating a user with Username and Passwords.where they are stored in a database.after successful authentication we are setting sessions and redirecting the user to the Dashboard/profile.And we maintain sessions wherever we need to protect the access.

 var Local = require('passport-local').Strategy;

 var User = require('../models/model');
  module.exports = function(passport) {
    passport.serializeUser(function(user, done) {
        done(null, user.id);
    });
    passport.deserializeUser(function(id, done) {
        User.findById(id, function(err, user) {
            done(err, user);
        });
     });



In the Above Code we have just initialize user from model schema of Database and passport as Local strategy.
I have used the module.exports to make seperate files in order to easily maintain the code and understanding.however it is connected to the server.js where we export the module.
serialize and deserialize is commonly used to deal with sessions handling.Stackoverflow answer is easy to understand,what does the abvove two function does.check it here

Creating and Naming a LocalStrategy:

 passport.use('signup', new LocalStrategy({
        usernameField : 'email',
        passwordField : 'password',
        passReqToCallback : true 
    },
    function(req, email, password, done) {

        User.findOne({ 'users.email' :  email }, function(err, user) {
            if (err)
                return done(err);
            if (user) {
                return done(null, false, req.flash('signupError', 'Email is already taken.'));
            } 
            else {
       var newUser            = new User();
                newUser.users.email    = email;
                newUser.users.password = password; 
                newUser.save(function(err) {
                    if (err)
                        throw err;
                    return done(null, newUser);
                });
               }
        });
    }));


By the above functionality we are creating a localstrategy named signup for user registration.Generally for basic Signup we need username/Email with password is allowed in passport.js but we could inspect additional parameters by passing to callback the req object,so when you submit the form the additional params can be obtained from req object.check for additional form fields passing for passport.js.Since we explicitly make the passReqToCallback : true option we could pass the req object and inspect it inside the passport.js function.

And the next step is to query the Database for the existing user and return either done with error or success message to set the session.

we are querying the user document and getting out the object as user and if it exist we pass it as an error to flash a Error message.Else create a new Object with the Database schema and populate the Database accordingly and invoke done with user object to set the session.

Login Strategy :

here we are just naming and creating a simple login functionality to authenticate the user to use the service and set the session.


  passport.use('login', new LocalStrategy({
        usernameField : 'email',
        passwordField : 'password',
        passReqToCallback : true 
         },
    function(req, email, password, done) { 
        User.findOne({ 'users.email' :  email }, function(err, user) {
            if (err)
                return done(err);
            if (!user)
                return done(null, false, req.flash('loginError', 'No user found.try Again'));               if(user.users.password != password)
               return done(null, false, req.flash('loginError', 'Password Error.try Again')); 
            
            return done(null, user); 

        });

    }));


In the above Strategy we defined as login and passing our Username/Email and Password,btw we could overrride and passreqcall back to use additional form fields via req object as said before.However here query on mongoose matters here.
we are finding only one record containing email/username and getting out the object as user as return from database.However we need to check the object because in mongoose,if no record is returned by database then the object would be null, so that we verify with if clause statements and Authenticate the user and set the session or flash the error message.

user.js(models) : 



 var mongoose = require('mongoose');
 var user = mongoose.Schema({
    users            : {
        email        : String,
        password     : String
  }
  });
  module.exports = mongoose.model('User', user);


A simple Mongoose scheme for Database.have a look below.Local for email and password.verify to check the user via Email.We would cover this Email Verfication for User later.

Define Routes Using Express : 

now let us create some Express route to handle the request from user and serve the view part.I am writing the Routes directly in Server.js file , you can to export the module and write and load as separate file.here passport.authenticate function is called to take care of the verification strategy.



  app.get("/",isLoggedIns,function(req,res){
res.render("index.ejs",{ message: req.flash('loginError') });
  });

  app.post("/login",passport.authenticate('login', {
successRedirect : '/profile', 
failureRedirect : '/',
failureFlash : true 
}));

  app.get("/signup",isLoggedIns,function(req,res){
res.render("signup.ejs",{ message: req.flash('signupError') });
  });

  app.post("/signup",passport.authenticate('signup', {
successRedirect : '/profile', 
failureRedirect : '/signup',
failureFlash : true 
}));
  app.get("/profile",isLoggedIn,function(req,res)
  {
res.render("profile.ejs",{user : req.user });

  });

  app.get('/logout',isLoggedIn, function(req, res) {
  req.logout();
res.redirect('/');
  });

  app.get("/*",function(req,res){
res.render("404.ejs");
  });

  function isLoggedIn(req, res, next) {

if (req.isAuthenticated())
return next();
res.redirect('/');
   }

   function isLoggedIns(req, res, next) {

if (req.isAuthenticated())
res.redirect('/profile');
else
return next();
   }



Few Words About EJS and Connect-Flash : 

Connect flash is used to flash the Error/Success message between the Authentication Passport and User.just the Error message (user defined) to the User Display.
Ejs Template is generally used for displaying our views as HTML and parsing the values as JSON and displaying it in front end dynamically.Learn more about EJS here.
Sample view index.ejs to parse the Error in Login :



      <div class="col-sm-12">
    <% if (message.length > 0) { %>
        <div class="alert alert-danger"><%= message %></div>
    <% } %>
       </div>

Database Connection and Establishment:

Learn Here to create Modulus.io Database and set up Remote Connection in my past post.

module.exports = {
  
   'url' : 'mongodb://username:password@proximus.modulusmongo.net:27017/abs' 
   
    };

About Security and Validation : 

I haven't verified any SQL injection for this Above Post,however you can use some packages to verify SQL injections and Execution commands that lets the server to open to Attack.Generally when you use eval() command,it gives hackers a well opportunity to Down the server and Application.i have escaped all the inputs provided by the user here.

Note:
Download the Code provided and Work it out.The given Code doesn't suits for production mode,here you can do lot of development works and make ready for production mode.Security is more concern for node.js because of exec() command.try to validate the user inputs and escape it and use it in your operations and perform Database operation.

Note For Downloading code :
If your downloading the code,

  • update the Credentials of Database in database.js file.
  • npm install the packages in your folder using package.json file given.
  • I have used Express session ,you can change the Session secret to implement in your projects.
  • to run execute node server.js command once you get into directory.

For Errors/Suggestions/Clarification/improvements/bugs drop mail to s.shivasurya@gmail.com .or connect/chat with me Facebook/twitter/G+ hangouts for errors/help/Bugs.Share is care.Do comments.

Wednesday, October 08, 2014

Create HTTPS over Express Node.js Server On LocalHost With OpenSSL

https connection was thought as More secured but After Heart Bleed.Probably http is more secure :p .And Recently you would heard that Google Prefers HTTPS connection for SEO boost up and Higher ranks on Google Search.Many guys switched over Https connections.However This is a Simple security for transferring of data with encrypted format.However both the Side have Public and Private Key to ensure safe transfer of data and Disallowing Hackers/Anonymous peoples to Access your data.



Reference : Download Demo 

Installation :

Before starting this module, i would like to go through some of My Last post on basic articles in node.js
Note : Here we are simply Creating Self Signed Certificate to create HTTPS connection.So,Browsers may Warn hackers could steal/compromise your Data.This is for Development purpose.For production purpose Buy SSL certificate from Godaddy or leading SSL providers and Set up in your server.


Procedure to generate SSL certificate:

1) Generate RSA key with Openssl

C:\> openssl genrsa 1024 > /path/to/key.pem
2) Execute the Below Command to Generate certificate SSL with your self signature.with passphrase

C:\> openssl req -new -key /path/to/key.pem -out csr.pem
C:\> openssl x509 -req -days 365 -in key.pem -signkey /path/to/file.pem -out /path/to/ssl.crt

Take these two files to your server directory (node project directory)

Server.js :

Set up connection with including https module and make it to listen to a port namely 443 for Secured connections.
with the help of fs module just provide the path of key.pem and ssl.crt,passphrase(optional)or else you will be requested in commandline when starting your server each time.



var fs = require('fs');
var https = require('https');
var express = require('express');
var app = express();

    app.get('/', function (req, res) {
    res.send("hello");
    });

https.createServer({
      key: fs.readFileSync('key.pem'),
      cert: fs.readFileSync('ssl.pem')
    }, app).listen(443);


Now issue npm server and start your server.now you will be prompted for passphrase and if it is correct your server is ready to take off!

now point to the browser and https://localhost:443/ will show some Error/Security message.simply accept it and test your server.note this happens because of creating self signed certificate.while going into production mode buy a commercial and register with certified SSL verifier.

Have a view at my secured https connection in localhost! with some message.just click advanced and proceed you may get your normal route.


Create HTTPS over Express Node.js Server On LocalHost With OpenSSL



Note : Here we are simply Creating Self Signed Certificate to create HTTPS connection.So,Browsers may Warn hackers could steal/compromise your Data.This is for Development purpose.For production purpose Buy SSL certificate from Godaddy or leading SSL providers and Set up in your server.


For Errors/bugs/suggestions ideas just comment below or mail me s.shivasurya@gmail.com or connect with me in Facebook/Google+/Twitter for more details.Share is care.

Wednesday, October 01, 2014

Working With Apiary.io - Create RESTful WebService Documentation Much Easier

Creating RESTful WebService Doesn't matters here.It matters about how you Provide Documentation for your Users/Developers to consume your web service in Rightful manner.You Need to Create Your Best out of for Documentation and clearly explain with some kind of Demo or Else Developers may seek through Stackoverflow and other forums to resolve their Problem and May hinder development of Apps.apiary.io provides default doc for every API project you create.

Learn Here to create RESTful Webservice using Node.js/Mongoose dB

Working With Apiary.io - Create RESTful WebService Documentation Much Easier


Getting Started :  Visit Demo

Create an Account at apiary.io website and create a Domain in your favour and you can find sample coding for notes API.
Either you can simply Edit that API content and implement yours or else just follow below ideas/procedures to build a Successful API Documentation.

Host and Version For API :


 FORMAT: 1A

 HOST: http://api.example.com/



Name your API with Short Description:




 # Film API
 Film API Web Service Done with Help of Node.js as Backend.


Create a group :


 # Group film
 This Route film can get recent films and post new films


Categorize Your API Routes :


 ## film Routes [/films]


This will automatically create as /film below for each route you define.

Define Each of Your Route :

Here indentation is more important to overcome Symantic Errors.Either 8 Space bar or two Tab key press.



 ### List popular film [GET]
 + Response 200 (application/json)

        [{
          "id": 1, "title": "Neethane En Ponvasantham","genre":"love"
        }, {
          "id": 2, "title": "Moonu - 3 Film","genre":love"

        }]


Actually above Hash(#) must be thrice for defining completely your route with a Name of the route with REQUEST TYPE in SQUARE BRACKET.

Next Step define your Response with HTTP Status code and with return type of Response.
and With Enter key followed with Two tab spaces start typing example results from server

POST request : 


 ### Create a film [POST]
 + Request (application/json)

        { "title": "Yaan","genre":"action" }

 + Response 201 (application/json)


        { "id": 3, "title": "Yaan","genre":"action" }


Routes With Parameters :

Generally Parameters are passed via URL(Actually here URI) to make a GET/DELETE/PUT request.you must clearly specify the Data type and purpose of Parameters to be passed and gain expected data.

Define A specific Routes to categorize since the same uri can be used for GET , POST , PUT.


 ## Film By ID [/film/{id}]
 A single Film Details is performed here

 + Parameters
     + id (required, number, `1`) ... Numeric `id` is unique value given to the Each Film.


As like before follow the indentation here too.Either 8 spaces/two tabs.

Define Each Route Below.


### Retrieve a film  [GET]
+ Response 200 (application/json)

    + Header

            X-My-Header: The Value

    + Body

            { "id": 2, "title": "Neethane En Ponvasantham","genre":"love" }

### Remove a film [DELETE]
+ Response 204




Thus We have Define a Successful doc for our Successful API with Apiary.io,Just preview with Apiary and make changes.

Have a look at my Simple Film API snapshot : visit for full Demo here

Working With Apiary.io - Create RESTful WebService Documentation Much Easier


The Above Screenshot of My API service for film API.

Caution : 
Don't Follow above Example codes,those are not formatted with tabs/Spaces
The Sample will be working only if you add correct host,and make sure of CORS policy of Ajax to run sample properly.Indentation for Response,Request is more important.Either(8 spaces/2 Tabs).

For bugs/comments/suggestions/errors comment below or mail me s.shivasurya@gmail.com or connect with me in Facebook/Twitter.Feel free to comment below.Share is care.

Sunday, September 21, 2014

Sending Email using G-Mail SMTP in Node.js - Mailer and Nodemailer

Sending Email is an important Aspect for applications for notifying users,registering users and Announcement to users.However Email keeps the Application and the User interactive and stay updated for recent changes and notify.Recently i have used G-Mail for sending mails for my client,However this is Easy in PHP,Where in node.js there are two popular library to do this and send mail to your clients pragmatically from node js application.mailer is depreciated in favor of nodemailer.if you need attractive html emails better learn any templating language EJS,JADE to integrate with the email.there are seperate npm for email templates too.

Sending Email using G-Mail SMTP in Node.js - Mailer and Nodemailer


Reference : Download app Zip | mailer | nodemailer

Installation : 

  • npm install nodemailer -g
  • npm install mailer -g
these both node js modules are popular one to send mails programaticaly.Recently i utilized mailer for my cloud project.however there are lot of API to send mails like sendgrid,mailgun and lot of mail exchange service.

Mailer : 

Mailer library is also efficient to use but it has been depreciated in favor of nodemailer library in github.However it is working well, and i am using it up in my recent cloud node project.Add your gmail account/password.




Node-mailer :


here just add nodemailer and create a variable ,with configuration details creating a Transport SMTP for mail service as Gmail,and here comes authentication with Gmail username and password. 




Integration with Express :

While you could simply integrate with express app with routes.Learn here to create Express App Routes.

 var http = require("http");
 var express = require("express");
 var app = express();

  app.get("/mailer",function(res,req){

   //your mailer logic here to send mail

  }); 
  app.get("/nodemailer",function(res,req){

   //your nodemailer logic here to send mail

  });

 http.createServer(app).listen(3000);


Thus we had integrate with express and tested our Email Sending application,I havent created any fault tolerant Exceptions but sure Errors will be thrown if something goes wrong.i am leaving it in your side to handle run time Errors.


Email App Screenshots :
  • have a look at the screenshot mail sent from my Node.js app via Gmail account SMTP.
  • i have logged the request and response of Mail.
  • Mails using both mailer and node mailer library.



You could attach mail attachments in your mail.read their documentation for complete details and you coud send HTML emails by using html as parameter,however there are still Email Template from Jade,HBS and EJS templates.

Thus we have covered two Library for Sending Email from node.js that too from our own Gmail account using SMTP.have a look at the Demo File and work it out.

For Errors/bugs/comments/suggestions just comment it out or mail me s.shivasurya@gmail.com or connect with me in Facebook/Twitter/Linkedin/Google+ and chat for more in detail regarding this post and integrations.share is care.Do comments.

Saturday, September 13, 2014

Getting Started With Ionic App - Android Application

Nowadays peoples get fascinated by using apps and even my friends review many apps from play store and share their experience with their besties :D . And recently i have attended a workshop on Mobile apps development and already working with ionic apps development.However there are lot of pitfalls in using html5 with phonegap while writing for native.The only thing is easy way to code and organize front end with backend.

Getting Started With Ionic App - Android Application


Installation :  

installing ionic in your system is very easy if you have node.js installed already and integrate it with Eclipse or any other ADT supported studio with plugins.

C:\> npm install -g ionic

Learn here to install node.js and using Eclipse IDE For android development.

Configuring on Windows :

Well i don't want to write too much of configuring steps here.all is simple short list goes below
  1. you must have installed Java and accessible Via commandline(javac and java)
  2. You must have installed ANT and accessible via commandline i.e simply specify the path till ant folder create as variable ANT_HOME
  3. You must have installed eclipse IDE(i had juno) with ADT updated with Android SDK and active emulators.
  4. now once again create ENV variable ANDROID_HOME specify the path till sdk folder
  5. Create three path variable with semicolon as terminator
    %ANT_HOME%\bin;
    %ANDROID_HOME%\tools;
    %ANDROID_HOME%\platform-tools;
Make sure everything works fine via CLI.However,this is common for both Windows/Linux.If you cant traces out wrong,similar doubts are answered in Stackoverflow forum.

Creating your first App : 

create a new directory and move into it using command prompt.here we are just creating a basic app and testing it locally on emulator installing the apk file into emulator from our ionic project.

  • C:\>ionic start demoapp tabs
    this creates demoapp folder with tabs app (previously demo files from ionic)
  • C:\>cd demoapp
  • C:\demoapp> ionic platform add android
    here we just add necessary build files for specific platform,since we are doing for android add key word android.This adds necessary bin,src config source files.
  • C:\demoapp>ionic build android
    just we are going to compile android application and create apk file from our source files.
  • C:\demoapp>ionic emulate android
    and finally this is gonna create a AVD(emulators) installing the apk file and launching the current activity on screen.
  • C:\demoapp>ionic run android
    and you can install it into android mobile if you have USB debuggin connected with internal/SD card storage under developer options in android os.
If all above steps consequently works ,then u may get compiled and ready to install the app.if any errors occurs in between,it may be fault in your path setting/installation.

Errors are going to be in Node.js exception format.you can track the errors and clear it with Stackoverflow website.

Screenshots :

Create a Demoapp folder.



Ionic start demoapp tabs command just download the starter template from github account and loads into the folder for you.


ionic platform add android : creates necessary build files for android project with java files and cardova plugins.

ionic build android : compiles the source and create necessary build files for android platform and dependencies


ionic emulate android : just creates avd for you and install apk file into the emulator and launches the main activity.


ionic run android : just deploy the android application directly into your android mobile while connecting with USB , With Unknown Source apps and USB debugging connected.look at screenshot below app on emulator.


Android Sample App running active device with installed app.




Download Demo files/install and work with project.However there are lot of disadvantages in hybrid app development.Your source codes are revealed when the apk file is decompiled :D in production.


For errors/bugs/suggestion comment below or mail me s.shivasurya@gmail.com or connect with me in Facebook/Twitter/Linkedin.Share is care.