Search This Blog

2015-12-21

Node JS debugging

Node Inspector is a debugger interface for Node.js applications.

Install the Node Inspector by the following command.
$ npm install -g node-inspector

Debug your application by the following command.The node-debug command will load Node Inspector in your default browser.
node-debug index.js

Use the debugger; in your code , to watch the values/debug , when you call node js app from HTML based client App.

2015-12-19

Install NodeJS without Admin Right

1)Download nodejs from below location and extract to some local folder
http://nodejs.org/dist/latest/

2)Download npm from the below location and extract to some local folder
https://github.com/npm/npm/releases

3)Set PATH Variable either by below command or from Environment variable setting in your computer, when you have access
set PATH=%PATH%;C:\NODE_LOC\;C:\NPM_LOC


4)Set up NPM when its behind proxy
npm config set proxy http://proxy:port
npm config set https-proxy https://proxy:port
or with credential
npm config set proxy http://username:password@proxy:port
npm config set https-proxy https://username:password@proxy:port


5)Test by the below command
>node -v
>npm -v
--------------------------------------------------------------------------------------------------------------
Check the current setting by following comment :
npm config ls -l

Remove the Proxy set as above step 4 :
npm config rm proxy npm config rm https-proxy

Some strange issues can be resolved by the following commands :
npm cache clean

2015-12-17

Mongodb Overview

Installation :

Download Mongodb from below location

https://www.mongodb.org/downloads#production

Custom installation, in order to set a simple installation location like C:\Manab\MongoDB

Set the Environment Variable(PATH) as below

C:\Manab\MongoDB\Server\3.0\bin

Run :

mongod.exe --dbpath C:\Manab\MongoDB\data

Open another command prompt and navigate to the below location

C:\>cd C:\Manab\MongoDB\Server\3.0\bin

and type mongo.exe

Useful commands :

>show dbs : To check all the dbs exist in your mongodb server

>db.hostInfo() : To get the host information

>db.stats() :-To get stats about mongodb server type

>use fastcuredb :-To create database,It will not display in 'show dbs' list, until have any document

>db :-To check your currently selected database

>db.delete.insert({"name":"Manab Sample DB"}) / db.delete.insert({"name":"Arpana Basu","location":"UK"})

>show collections :-To see all the collections in the db

>db.createCollection("delete2") :-To create a new collection

>db.createCollection("delete3", { capped : true, autoIndexID : true, size : 6142800, max : 10000 } ) :-Create collection with parameter, can be seen by 'db.delete3.stats()'

>db.COLLECTION_NAME.find() /db.COLLECTION_NAME.find().pretty():-To query data either non formatted or formatted way(pretty), i.e db.delete.find()

>db.delete.find({"name":"Arpana Basu"}) /db.delete.find({"name":/Arpana/})/ db.delete.find({"name":/.*Arpana.*/,"location":"UK"}) /db.delete.find().limit(1)/db.delete.find().limit(1).skip(1)/db.delete.find().limit(1).sort({"name":-1})

>db.delete.update({"name":"Arpana Basu"},{$set:{"name":"Arpana B","location":"England"}})

>db.delete.remove({"name":"Arpana B"}) /db.delete.remove({"name":"Arpana B"},1)/ db.delete.remove()

>db.delete.ensureIndex({"name":1,"location":-1})
-db.delete.update({"name":"Arpana Basu"},{$set:{"age":30}})
-db.delete.update({"name":"Manab Sample DB"},{$set:{"location":"UK","age":34}})

>db.delete.aggregate([{$group : {_id : "$location", people : {$sum : 1}}}])--Sum of people group by Country
db.delete.aggregate([{$group : {_id : "$location", people : {$avg : 1}}}])--avg of people group by Country

>db.COLLECTION_NAME.drop() :- To delete collection i.e db.delete3.drop()

>db.dropDatabase :-to drop a existing database

>db.help()

Additional DB level commands :

        db.adminCommand(nameOrDocument) - switches to 'admin' db, and runs command [ just calls db.runCommand(...) ]
        db.auth(username, password)
        db.cloneDatabase(fromhost)
        db.commandHelp(name) returns the help for the command
        db.copyDatabase(fromdb, todb, fromhost)
        db.createCollection(name, { size : ..., capped : ..., max : ... } )
        db.createUser(userDocument)
        db.currentOp() displays currently executing operations in the db
        db.dropDatabase()
        db.eval() - deprecated
        db.fsyncLock() flush data to disk and lock server for backups
        db.fsyncUnlock() unlocks server following a db.fsyncLock()
        db.getCollection(cname) same as db['cname'] or db.cname
        db.getCollectionInfos()
        db.getCollectionNames()
        db.getLastError() - just returns the err msg string
        db.getLastErrorObj() - return full status object
        db.getLogComponents()
        db.getMongo() get the server connection object
        db.getMongo().setSlaveOk() allow queries on a replication slave server
        db.getName()
        db.getPrevError()
        db.getProfilingLevel() - deprecated
        db.getProfilingStatus() - returns if profiling is on and slow threshold
        db.getReplicationInfo()
        db.getSiblingDB(name) get the db at the same server as this one
        db.getWriteConcern() - returns the write concern used for any operations on this db, inherited from server object if set
        db.hostInfo() get details about the server's host
        db.isMaster() check replica primary status
        db.killOp(opid) kills the current operation in the db
        db.listCommands() lists all the db commands
        db.loadServerScripts() loads all the scripts in db.system.js
        db.logout()
        db.printCollectionStats()
        db.printReplicationInfo()
        db.printShardingStatus()
        db.printSlaveReplicationInfo()
        db.dropUser(username)
        db.repairDatabase()
        db.resetError()
        db.runCommand(cmdObj) run a database command.  if cmdObj is a string, turns it into { cmdObj : 1 }
        db.serverStatus()
        db.setLogLevel(level,)
        db.setProfilingLevel(level,) 0=off 1=slow 2=all
        db.setWriteConcern( ) - sets the write concern for writes to the db
        db.unsetWriteConcern( ) - unsets the write concern for writes to the db
        db.setVerboseShell(flag) display extra information in shell output
        db.shutdownServer()
        db.stats()
        db.version() current version of the server

Additional Collection Level Commands :

        db.test.find().help() - show DBCursor help
        db.test.count()
        db.test.copyTo(newColl) - duplicates collection by copying all documents to newColl; no indexes are copied.
        db.test.convertToCapped(maxBytes) - calls {convertToCapped:'test', size:maxBytes}} command
        db.test.dataSize()
        db.test.distinct( key ) - e.g. db.test.distinct( 'x' )
        db.test.drop() drop the collection
        db.test.dropIndex(index) - e.g. db.test.dropIndex( "indexName" ) or db.test.dropIndex( { "indexKey" : 1 } )
        db.test.dropIndexes()
        db.test.ensureIndex(keypattern[,options])
        db.test.explain().help() - show explain help
        db.test.reIndex()
        db.test.find([query],[fields]) - query is an optional query filter. fields is optional set of fields to return.
                                                      e.g. db.test.find( {x:77} , {name:1, x:1} )
        db.test.find(...).count()
        db.test.find(...).limit(n)
        db.test.find(...).skip(n)
        db.test.find(...).sort(...)
        db.test.findOne([query])
        db.test.findAndModify( { update : ... , remove : bool [, query: {}, sort: {}, 'new': false] } )
        db.test.getDB() get DB object associated with collection
        db.test.getPlanCache() get query plan cache associated with collection
        db.test.getIndexes()
        db.test.group( { key : ..., initial: ..., reduce : ...[, cond: ...] } )
        db.test.insert(obj)
        db.test.mapReduce( mapFunction , reduceFunction , )
        db.test.aggregate( [pipeline], ) - performs an aggregation on a collection; returns a cursor
        db.test.remove(query)
        db.test.renameCollection( newName , ) renames the collection.
        db.test.runCommand( name , ) runs a db command with the given name where the first param is the collection name
        db.test.save(obj)
        db.test.stats({scale: N, indexDetails: true/false, indexDetailsKey: , indexDetailsName: })
        db.test.storageSize() - includes free space allocated to this collection
        db.test.totalIndexSize() - size in bytes of all the indexes
        db.test.totalSize() - storage allocated for all data and indexes
        db.test.update(query, object[, upsert_bool, multi_bool]) - instead of two flags, you can pass an object with fields: upsert, multi
        db.test.validate( ) - SLOW
        db.test.getShardVersion() - only for use with sharding
        db.test.getShardDistribution() - prints statistics about data distribution in the cluster
        db.test.getSplitKeysForChunks( ) - calculates split points over all chunks and returns splitter function
        db.test.getWriteConcern() - returns the write concern used for any operations on this collection, inherited from server/db if set
        db.test.setWriteConcern( ) - sets the write concern for writes to the collection
        db.test.unsetWriteConcern( ) - unsets the write concern for writes to the collection

Mongodb Datatype :
MongoDB supports many datatypes whose list is given below:

String : This is most commonly used datatype to store the data. String in mongodb must be UTF-8 valid.
Integer : This type is used to store a numerical value. Integer can be 32 bit or 64 bit depending upon your server.
Boolean : This type is used to store a boolean (true/ false) value.
Double : This type is used to store floating point values.
Min/ Max keys : This type is used to compare a value against the lowest and highest BSON elements.
Arrays : This type is used to store arrays or list or multiple values into one key.
Timestamp : ctimestamp. This can be handy for recording when a document has been modified or added.
Object : This datatype is used for embedded documents.
Null : This type is used to store a Null value.
Symbol : This datatype is used identically to a string however, it's generally reserved for languages that use a specific symbol type.
Date  : This datatype is used to store the current date or time in UNIX time format. You can specify your own date time by creating object of Date and passing day, month, year into it.
Object ID : This datatype is used to store the document’s ID.
Binary data : This datatype is used to store binay data.
Code : This datatype is used to store javascript code into document.
Regular expression : This datatype is used to store regular expression

2015-12-05

Node JS Streams

A stream is an abstract interface implemented by various objects in Node.js. For example a request to an HTTP server is a stream. Streams are readable, writable, or both. All streams are instances of EventEmitter

You can load the Stream base classes by doing require('stream'). There are base classes provided for Readable streams, Writable streams, Duplex streams(both readable and writable), and Transform streams(Transform streams are Duplex streams where the output is in some way computed from the input).

Each type of Stream is an EventEmitter instance and throws several events at different instance of times. For example, some of the commonly used events are:
  • data - This event is fired when there is data is available to read.
  • end - This event is fired when there is no more data to read.
  • error - This event is fired when there is any error receiving or writing data.
  • finish - This event is fired when all data has been flushed to underlying system
Example:

Step 1: Run "npm init"

Step 2 : Insert all required parameter in 'npm init' and create the start page 'index.js'.

Step 3: index.js will be as below

var fs = require("fs");
var data = '';


// Create a readable stream
var readerStream = fs.createReadStream('myinputfile.txt');

// Set the encoding to be utf8.
readerStream.setEncoding('UTF8');

// Handle stream events --> data, end, and error
readerStream.on('data', function(chunk) {//The data event is fired when there is data is available to read.
   console.log('data event from Read Stream');
   data += chunk;
});

readerStream.on('end',function(){//The end event is fired when there is no more data to read.

   console.log('end event from Read Stream: ' + data);
});

readerStream.on('finish',function(){//The finish event is fired when all data has been flushed to underlying system
   console.log('finish event from Read Stream: ' +data);
});

readerStream.on('error', function(err){//The Error event is fired when there is any error receiving or writing data.
   console.log(err.stack);
});

console.log("Program Ended for Read Stream");
///////---------------------------------Write Stream--------------------------------------
console.log(data);
data = 'Simply Easy Learning';

// Create a writable stream
var writerStream = fs.createWriteStream('output.txt');

// Write the data to stream with encoding to be utf8
writerStream.write(data,'UTF8');

// Mark the end of file
writerStream.end();

// Handle stream events --> finish, and error
writerStream.on('finish', function() {
    console.log("Write completed from write Stream.");
});

writerStream.on('error', function(err){
   console.log(err.stack);
});

console.log("Program Ended for write stream");

Step 4: Create a new file 'myinputfile.txt' within the root folder with the text  'Hi, This is Manab'

Step 5:Run the application by 'node index.js'

Step 6: You could see a new file 'output.txt' has been generated within the root and below is the output in command prompt

Node JS File System

Node File System (fs) module can be imported using following syntax:
var fs = require("fs")
Every method in fs module have synchronous as well as asynchronous form. Asynchronous methods takes a last parameter
as completion function callback and first parameter of the callback function is error. It is preferred to use
asynchronous method instead of synchronous method as former never block the program execution where as the second one
does.
-----------------------------------------------------
fs.open(path, flags[, mode], callback):To open a file in asynchronous mode
var fs = require("fs");
// Asynchronous - Opening File
console.log("Going to open file!");
fs.open('input.txt', 'r+', function(err, fd) {
   if (err) {
       return console.error(err);
   }
  console.log("File opened successfully!");    
});
Modes-3 main modes(r-read,w-write,a-append.when suffix is s, it indicates synchonousmode,when + ,it indicates both
operation)
-----------------------------------------------------
fs.read(fd, buffer, offset, length, position, callback):To read the file
var fs = require("fs");
var buf = new Buffer(1024);
console.log("Going to open an existing file");
fs.open('input.txt', 'r+', function(err, fd) {
   if (err) {
       return console.error(err);
   }
   console.log("File opened successfully!");
   console.log("Going to read the file");
   fs.read(fd, buf, 0, buf.length, 0, function(err, bytes){
      if (err){
         console.log(err);
      }
      console.log(bytes + " bytes read");
     
      // Print only read bytes to avoid junk.
      if(bytes > 0){
         console.log(buf.slice(0, bytes).toString());
      }
   });
});
-----------------------------------------------------
fs.writeFile(filename, data[, options], callback):This method will over-write the file if file already exists.
var fs = require("fs");
console.log("Going to write into existing file");
fs.writeFile('input.txt', 'Simply Easy Learning!',  function(err) {
   if (err) {
       return console.error(err);
   }
   console.log("Data written successfully!");
   console.log("Let's read newly written data");
   fs.readFile('input.txt', function (err, data) {
      if (err) {
         return console.error(err);
      }
      console.log("Asynchronous read: " + data.toString());
   });
});
-----------------------------------------------------
fs.close(fd, callback):To close an opened file
var fs = require("fs");
var buf = new Buffer(1024);
console.log("Going to open an existing file");
fs.open('input.txt', 'r+', function(err, fd) {
   if (err) {
       return console.error(err);
   }
   console.log("File opened successfully!");
   console.log("Going to read the file");
   fs.read(fd, buf, 0, buf.length, 0, function(err, bytes){
      if (err){
         console.log(err);
      }
      // Print only read bytes to avoid junk.
      if(bytes > 0){
         console.log(buf.slice(0, bytes).toString());
      }
      // Close the opened file.
      fs.close(fd, function(err){
         if (err){
            console.log(err);
         }
         console.log("File closed successfully.");
      });
   });
});

------------------------------------------------------
fs.stat(path, callback):To get the information about a file
var fs = require("fs");
console.log("Going to get file info!");
fs.stat('input.txt', function (err, stats) {
   if (err) {
       return console.error(err);
   }
   console.log(stats);
   console.log("Got file info successfully!");
  
   // Check file type
   console.log("isFile ? " + stats.isFile());
   console.log("isDirectory ? " + stats.isDirectory());   
});
-------------------------------------------------------
fs.unlink(path, callback): To delete a file
var fs = require("fs");
console.log("Going to delete an existing file");
fs.unlink('input.txt', function(err) {
   if (err) {
       return console.error(err);
   }
   console.log("File deleted successfully!");
});
-------------------------------------------------------
fs.ftruncate(fd, len, callback) : To truncate an opened file
var fs = require("fs");
var buf = new Buffer(1024);
console.log("Going to open an existing file");
fs.open('input.txt', 'r+', function(err, fd) {
   if (err) {
       return console.error(err);
   }
   console.log("File opened successfully!");
   console.log("Going to truncate the file after 10 bytes");
  
   // Truncate the opened file.
   fs.ftruncate(fd, 10, function(err){
      if (err){
         console.log(err);
      }
      console.log("File truncated successfully.");
      console.log("Going to read the same file");
      fs.read(fd, buf, 0, buf.length, 0, function(err, bytes){
         if (err){
            console.log(err);
         }
         // Print only read bytes to avoid junk.
         if(bytes > 0){
            console.log(buf.slice(0, bytes).toString());
         }
         // Close the opened file.
         fs.close(fd, function(err){
            if (err){
               console.log(err);
            }
            console.log("File closed successfully.");
         });
      });
   });
});
-------------------------------------------------------
fs.mkdir(path[, mode], callback): To create a directory
var fs = require("fs");
console.log("Going to create directory /tmp/test");
fs.mkdir('/tmp/test',function(err){
   if (err) {
       return console.error(err);
   }
   console.log("Directory created successfully!");
});
-------------------------------------------------------
fs.rmdir(path, callback) : To remove a directory
var fs = require("fs");
console.log("Going to delete directory /tmp/test");
fs.rmdir("/tmp/test",function(err){
   if (err) {
       return console.error(err);
   }
   console.log("Going to read directory /tmp");
   fs.readdir("/tmp/",function(err, files){
      if (err) {
          return console.error(err);
      }
      files.forEach( function (file){
          console.log( file );
      });
   });
});
-------------------------------------------------------
fs.readdir(path, callback) : To read a directory
var fs = require("fs");
console.log("Going to read directory /tmp");
fs.readdir("/tmp/",function(err, files){
   if (err) {
       return console.error(err);
   }
   files.forEach( function (file){
       console.log( file );
   });
});

Express

Express is a minimal and flexible Node.js web application framework that provides a robust set of features to develop web and mobile applications. It allows to set up middlewares to respond to HTTP  Requests and defines a routing table which is used to perform different action based on HTTP Method and URL.It allows to dynamically render HTML Pages based on passing arguments to templates.


To Install Mean Stack run the following comment
  • npm install -g mean-cli
and make the initial structure of the project by following command
  • mean init yourNewApp
or, Run below command to install only the Express
  • npm install express --save                 
There are following important modules which you should install along with express:
  • body-parser - This is a node.js middleware for handling JSON, Raw, Text and URL encoded form data.
  • cookie-parser - Parse Cookie header and populate req.cookies with an object keyed by the cookie names.
  • multer - This is a node.js middleware for handling multipart/form-data.


  • $ npm install body-parser --save
  • $ npm install cookie-parser --save
  • $ npm install multer --save


Step 1: Run "npm init"

Step 2 : Insert all required parameter in 'npm init' and create the start page 'index.js'.

Step 3:Amend the package.json as below

{
  "name": "demoforexpress",
  "version": "1.0.0",
  "description": "Demo for Express",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "MRB",
  "license": "ISC",
  "dependencies": {
    "body-parser": "^1.14.1",
    "cookie-parser": "^1.4.0",
    "express": "^4.13.3",
    "multer": "^1.1.0"
  }
}

Step 4: index.js will be as below

var express = require('express');
var app = express();
app.get('/', function (req, res) {
 console.log("Get method Called")
   res.send('Response from GET method');
})
// This responds a POST request for the homepage
app.post('/', function (req, res) {
   console.log("Post method called");
   res.send('Response from POST method');
})
// This responds a DELETE request for the /del_user page.
app.delete('/del_user', function (req, res) {
   console.log("delete method called for /del_user");
   res.send('Response from Delete Method');
})
// This responds a GET request for the /list_user page.
app.get('/list_user', function (req, res) {
   console.log("get Method called for /list_user");
   res.send('response from GET for /list_user');
})
// This responds a GET request for abcd, abxcd, ab123cd, and so on
app.get('/ab*cd', function(req, res) {  
   console.log("Got a GET request for /ab*cd");
   res.send('Response from Page Pattern Match');
})
var server = app.listen(8081, function () {
  var host = server.address().address
  var port = server.address().port
  console.log("Example app listening at http://%s:%s", host, port)
})




Step 5: Run the application by 'node index.js' , test in rest client and see the responses in console.

----------------------------More on Express-------------------------------------
package.json

{
  "name": "demoforexpress",
  "version": "1.0.0",
  "description": "Demo for Express",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "MRB",
  "license": "ISC",
  "dependencies": {
    "body-parser": "^1.14.1",
    "cookie-parser": "^1.4.0",
    "express": "^4.13.3",
    "multer": "^1.1.0",
    "express-session":"*",
    "connect-busboy":"*",
    "path":"*",
    "fs-extra":"*",
    "formidable":"*"
  }
}

index.js

// fsutil file createnew index.js 2000
var express = require('express');
var app = express();
///-------------Basic Authentication-----------------------
var loginuser;
// Authenticator
app.use(function(req, res, next) {
    var auth;
   
    // check whether an autorization header was send   
    if (req.headers.authorization) {
      // only accepting basic auth, so:
      // * cut the starting "Basic " from the header
      // * decode the base64 encoded username:password
      // * split the string at the colon
      // -> should result in an array
      auth = new Buffer(req.headers.authorization.substring(6), 'base64').toString().split(':');
  
    }
    // checks if:
    // * auth array exists
    // * first value matches the expected user
    // * second value the expected password
    if ((!auth || auth[0] !== 'testuser' || auth[1] !== 'testpassword') && (!auth || auth[0] !== 'testuser2' || auth[1] !== 'testpassword2'))
    {
        // any of the tests failed
        // send an Basic Auth request (HTTP Code: 401 Unauthorized)
        res.statusCode = 401;
        // MyRealmName can be changed to anything, will be prompted to the user
        res.setHeader('WWW-Authenticate', 'Basic realm="MyRealmName"');
        // this will displayed in the browser when authorization is cancelled
        res.end('Unauthorized');
    } else {
        // continue with processing, user was authenticated
        loginuser=auth[0];
        next();
    }
    // if (!auth || auth[0] !== 'testuser' || auth[1] !== 'testpassword')
    // {
    //     // any of the tests failed
    //     // send an Basic Auth request (HTTP Code: 401 Unauthorized)
    //     res.statusCode = 401;
    //     // MyRealmName can be changed to anything, will be prompted to the user
    //     res.setHeader('WWW-Authenticate', 'Basic realm="MyRealmName"');
    //     // this will displayed in the browser when authorization is cancelled
    //     res.end('Unauthorized');
    // } else {
    //     // continue with processing, user was authenticated
    //     loginuser=auth[0];
    //     next();
    // }
});

////------------------------------End Authentication-------------------------------
app.get('/', function (req, res) {
 console.log("Get method Called")
   res.send('Response from GET method');
})
// This responds a POST request for the homepage
app.post('/', function (req, res) {
   console.log("Post method called");
   res.send('Response from POST method');
})
// This responds a DELETE request for the /del_user page.
app.delete('/del_user', function (req, res) {
   console.log("delete method called for /del_user");
   res.send('Response from Delete Method');
})
// This responds a GET request for the /list_user page.
app.get('/list_user', function (req, res) {
   console.log("get Method called for /list_user");
   res.send('response from GET for /list_user');
})
// This responds a GET request for abcd, abxcd, ab123cd, and so on
app.get('/ab*cd', function(req, res) {  
   console.log("Got a GET request for /ab*cd");
   res.send('Response from Page Pattern Match');
})
//------------------Cookie---------------------------
//This responds a GET request for cookie
var cookieParser = require('cookie-parser');
app.use(cookieParser());
app.get('/cookie', function (req, res) {
  //res.clearCookie("mycookie");
  res.cookie("mycookie","manab",{maxAge:864000000});
  res.setHeader('Content-Type', 'text/plain');
  res.send('Response from GET Cookie method ' +JSON.stringify(req.cookies));
})
//------------------Session---------------------------
//This responds a GET request for session
var session = require('express-session');
app.use(cookieParser());
app.use(session({ secret: 'loginuser', cookie: { maxAge: 60000 }}));
app.get('/session', function (req, res) {
var sess = req.session;
if (sess.views) {
    sess.views++
    res.setHeader('Content-Type', 'text/html')
    res.write('views: ' + sess.views + '
')
    res.write('expires in: ' + (sess.cookie.maxAge / 1000) + 's
')
    res.end()
  } else {
    sess.views = 1
    res.end('welcome to the session demo. refresh!')
  }
})
//------------------Uploading File---------------------------
//HTML For Uploading File
/*<!DOCTYPE html>
<html lang="en" ng-app="APP">
<head>
    <meta charset="UTF-8">
    <title>angular file upload</title>
</head>
<body>
        <form method='post' action='upload' enctype="multipart/form-data">
        <input type='file' name='fileUploaded'>
        <input type='submit'>
 </body>
</html>*/


var busboy = require('connect-busboy'); //middleware for form/file upload
var path = require('path');     //used for file path
var fs = require('fs-extra');       //File System - for file manipulation
app.use(busboy());
app.use(express.static(path.join(__dirname, 'public')));
//------------Without formidable--------
// app.route('/upload')
//     .post(function (req, res, next) {
//         var fstream;
//         req.pipe(req.busboy);
//         req.busboy.on('file', function (fieldname, file, filename) {
//             console.log("Uploading: " + filename);
//             //Path where image will be uploaded
//             fstream = fs.createWriteStream(__dirname + '/img/' + filename);
//             file.pipe(fstream);
//             fstream.on('close', function () {   
//                 console.log("Upload Finished of " + filename);             
//                 res.redirect('back');           //where to go next
//             });
//         });
//     });
//--------------------------------------
//------------------Uploading File With formidable
var bodyParser=require("body-parser");
var formidable = require("formidable");
app.use(bodyParser({defer: true}));
 app.route('/upload')
 .post(function (req, res, next) {
  var form = new formidable.IncomingForm();
    //Formidable uploads to operating systems tmp dir by default
    form.uploadDir = "./img";       //set upload directory
    form.keepExtensions = true;     //keep file extension
    form.parse(req, function(err, fields, files) {
        res.writeHead(200, {'content-type': 'text/plain'});
        res.write('received upload:\n\n');
        console.log("form.bytesReceived");
        //TESTING
        console.log("file size: "+JSON.stringify(files.fileUploaded.size));
        console.log("file path: "+JSON.stringify(files.fileUploaded.path));
        console.log("file name: "+JSON.stringify(files.fileUploaded.name));
        console.log("file type: "+JSON.stringify(files.fileUploaded.type));
        console.log("astModifiedDate: "+JSON.stringify(files.fileUploaded.lastModifiedDate));
        //Formidable changes the name of the uploaded file
        //Rename the file to its original name
        fs.rename(files.fileUploaded.path, './img/'+files.fileUploaded.name, function(err) {
        if (err)
            throw err;
          console.log('renamed complete'); 
        });
          res.end();
    });
});

//----------------------------------------------------
var server = app.listen(8081, function () {
  var host = server.address().address
  var port = server.address().port
  console.log("Example app listening at http://%d:%d", host, port)
})

Create server using HTTP,Express and HAPI

HTTP:
var http=require('http');
var handlerMethod=function(req,res){
 res.end("Hello, This is a message from the handler of web server of");
}
http.createServer(handlerMethod).listen(1234,'localhost');
console.log("Http Server is running");

Express:
var express = require('express');
var app = express();
app.get(*,function(req,res){
 res.end("response from server");
});
var server = app.listen(3000, function(){
    console.log('Listening on port %d', server.address().port);
});

Hapi:
var Hapi = require('hapi');
var server = new Hapi.Server(3000);//port number during initialize
server.start(function () {
    console.log('Server running at:', server.info.uri);
});

Difference between req.params,req.query and req.body :

(req.params) Checks route params, ex: /user/:id
(req.query) Checks query string params, ex: ?id=12 Checks urlencoded body params
(req.body), ex: id=12 To utilize urlencoded request bodies, req.body should be an object. This can be done by using the _express.bodyParser middleware.

Use Basic Authentication in the above example from Adv Rest Client:
URL-http://localhost:8081/
Header would be -Authorization: Basic dGVzdHVzZXI6dGVzdHBhc3N3b3Jk
To change the Authorization,Click on Form ->'Edit Value' for authorization header->Basic

Difference between res.end and res.send :
res.send('Response from POST method');-automatically assumes a Content-Type of html
res.end('Response from POST method');-so no assumptions are made for the Content-Type

Send cookie in response in the above example
use- cookie-parser to set and get cookie in request and response
call -http://localhost:8081/cookie to check also the request cookie which is firsttime set by response cookie


Output - Response from GET Cookie method {"_ga":"GA1.1.2097241217.1451846328","mycookie":"manab"}

Use Session Variable in the above example:
call http://localhost:8081/session from different type of browser , IE+Chrome
Delete Moudles which is can't be deleted for alonger file path name
npm install rimraf -g
rimraf node_modules

Upload file:
ref:http://stackoverflow.com/questions/23691194/node-express-file-upload
node index.js and open http://localhost:8081/index.html
Two example , first one is commented and without formidable , second one with formidable package

2015-11-16

Node.js - Event Loop and Event Emitter

Node.js uses events heavily and it is also one of the reasons why Node.js is pretty fast compared to other similar technologies. As soon as Node starts its server, it simply initiates its variables, declares functions and then simply waits for event to occur.

Node.js has multiple in-built events available through events module and EventEmitter class which is used to bind events.

EventEmitter provides multiple properties like on and emit. on property is used to bind a function with the event and emit is used to fire an event.

Below is an example :

Step 1: Run "npm init"

Step 2 : Insert all required parameter in 'npm init' and create the start page 'index.js'.
Step 3: index.js will be as below

// Import events module
var events = require('events');
// Create an eventEmitter object
var eventEmitter = new events.EventEmitter();
// Create an event handler as follows
var connectHandler = function connected() {
   console.log('connection succesful.');
 
   // Fire the data_received event
   eventEmitter.emit('data_received');
}
// Bind the connection event with the handler
eventEmitter.on('connection', connectHandler);

// Bind the data_received event with the anonymous function
eventEmitter.on('data_received', function(){
   console.log('data received succesfully.');
});
// Fire the connection event
var eventListeners = require('events').EventEmitter.listenerCount(eventEmitter,'connection');
console.log(eventListeners + " Listner(s) listening to connection event");
eventListeners = require('events').EventEmitter.listenerCount(eventEmitter,'data_received');
console.log(eventListeners + " Listner(s) listening to data_received event");
console.log("Listner Count Ended...............");
eventEmitter.emit('connection');
console.log("Connection Event Ended...............");
eventEmitter.emit('data_received');
console.log("data_received Event Ended...............");
eventEmitter.removeAllListeners();
eventEmitter.emit('connection');
console.log("Program Ended...............");
Step 5: Run the application by 'node index.js' and see the responses in console.



 
 

2015-11-14

Angular JS Routing

The routing functionality added by this step is provided by angular in the ngRoute module, which is distributed separately from the core Angular framework.

Step 1) Finish Serving a HTML file from Node JS

    Step 2) Create folder and files as below
Step 3)about.html

<div class="jumbotron text-center">
    <h1>About Page</h1>

    <p>{{ message }}</p>
</div>

Step 4) contact.html
<div class="jumbotron text-center">
    <h1>Contact Page</h1>

    <p>{{ message }}</p>
</div>

Step 5)error.html
<div class="jumbotron text-center">
    <h1>Error Page</h1>

    <p>{{ message }}</p>
</div>

Step 6)home.html

<div class="jumbotron text-center">
    <h1>Home Page</h1>

    <p>{{ message }}</p>
</div>

Step 7)routing.html

<!-- index.html -->
<!DOCTYPE html>
<html>
<head>
   
    <!-- load bootstrap and fontawesome via CDN -->
    <link rel="stylesheet" href="https://netdna.bootstrapcdn.com/bootstrap/3.0.0/css/bootstrap.min.css" />
    <link rel="stylesheet" href="https://netdna.bootstrapcdn.com/font-awesome/4.0.0/css/font-awesome.css" />

    <!-- load angular and angular route via CDN -->
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.25/angular.min.js"></script>
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.25/angular-route.js"></script>
    
    
    <script src="routing.js"></script>
</head>
<body ng-app="myApp" ng-controller="mainController">
    <!-- HEADER AND NAVBAR -->
    <header>
        <nav class="navbar navbar-default">
            <div class="container">
                <div class="navbar-header">
                    <a class="navbar-brand" href="/">Angular Routing Example</a>
                </div>

                <ul class="nav navbar-nav navbar-right">
                    <li><a href="#!/"><i class="fa fa-home"></i> Home</a></li>
                    <li><a href="#!/about"><i class="fa fa-shield"></i> About</a></li>
                    <li><a href="#!/contact/07448587873"><i class="fa fa-comment"></i> Contact</a></li>
                </ul>
            </div>
        </nav>
    </header>

    <!-- MAIN CONTENT AND INJECTED VIEWS -->
    <div id="main">
        {{ message }}

        <div ng-view></div>
        <!-- angular templating -->
        <!-- this is where content will be injected -->

    </div>

</body>
</html>


Step 8)routing.js

// create the module and name it myApp
// also include ngRoute for all our routing needs
var myApp = angular.module('myApp', ['ngRoute']);

// configure our routes
myApp.config(function ($routeProvider, $locationProvider) {
    $routeProvider

        // route for the home page
        .when('/', {
            templateUrl: 'pages/home.html',
            controller: 'mainController'
        })

        // route for the about page
        .when('/about', {
            templateUrl: 'pages/about.html',
            controller: 'aboutController'
        })

        // route for the contact page
        .when('/contact/:id', {
            templateUrl: 'pages/contact.html',
            controller: 'contactController'
        })
        .otherwise({   // This is when any route not matched
            templateUrl: 'pages/error.html',
            controller: 'errorController'
        })
    $locationProvider.html5Mode(false).hashPrefix('!'); // This is for Hashbang Mode
});

// create the controller and inject Angular's $scope
myApp.controller('mainController', function ($scope) {
    // create a message to display in our view
    $scope.message = 'Everyone come and see how good I look!';
});

myApp.controller('aboutController', function ($scope) {
    $scope.message = 'Look! I am an about page.';
});

myApp.controller('contactController', function ($scope, $routeParams) {
    $scope.message = 'Contact us! JK. This is just a demo.'  +$routeParams.id;
});

myApp.controller('errorController', function ($scope) {
    $scope.message = '404 : Not Found';
});


Step 9)run your code using 'node index.js' and Open your browser and try browsing your routing.html file (http://localhost:7879/routing.html)



2015-11-01

AngularJS

AngularJS lets you extend HTML vocabulary for your application. The resulting environment is extraordinarily expressive, readable, and quick to develop.

It could be downloaded from https://angularjs.org/

Sample Application:

1)Create 3 file index.html,app.js and appcontroller.js

2)index.html:
<!DOCTYPE html>
<html>
<head>
    <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"></script>
    <script src="app.js"></script>
    <script src="appcontroller.js"></script>
</head>
<body>
    <form ng-app="myApp" ng-controller="customersCtrl" name="myForm" novalidate>
<!-- ng-app Defines the root element of an application.  -->
        <ul>
            <li ng-repeat="x in names| orderBy : 'Country'"> <!-- ng-repeat : Defines a template for each data in a collection.  -->
               <!-- AngularJS Filters:
                currency- Format a number to a currency format.
                filter- Select a subset of items from an array.
                lowercase- Format a string to lower case.
                orderBy- Orders an array by an expression.
                uppercase- Format a string to upper case.  -->
                <span ng-show="x.Country != 'France'"> <!-- ng-show : Shows or hides HTML elements.  -->

                    {{ x.Name +' , '+ x.City +', ' + x.Country|uppercase }}
                <input type="text" ng-model="x.City"> <!-- ng-model: Binds the value of HTML controls to application data.  -->
               
                    <button ng-click="getCity(x.City)">Click Me</button>
                    <!-- AngularJS support the following events:
                    •ng-click
                    •ng-dbl-click
                    •ng-mousedown
                    •ng-mouseenter
                    •ng-mouseleave
                    •ng-mousemove
                    •ng-keydown
                    •ng-keyup
                    •ng-keypress
                    •ng-change -->


                </span>
            </li>
        </ul>
        <p>Email:<br>
          <input type="email" name="email" ng-model="email" id="myemail" required>
          <span style="color:red" ng-show="myForm.email.$dirty && myForm.email.$invalid">
          <span ng-show="myForm.email.$error.required">Email is required.</span>
          <span ng-show="myForm.email.$error.email">Invalid email address.</span>
          </span>
        </p>
        <!-- Validation
        $dirty- The user has interacted with the field.
        $valid- The field content is valid.
        $invalid- The field content is invalid.
        $pristine- User has not interacted with the field yet.  -->

     
    </form>

</body>
</html>


2)app.js

var app = angular.module('myApp', []); 
// angular.module() -Creates, registers, or retrieves an AngularJS module

3)appcontroller.js

app.controller('customersCtrl', function ($scope, $http) {
   
    $http.get("http://www.w3schools.com/angular/customers.php")
    .success(function (response) { $scope.names = response.records; });
   
    $scope.getCity = function (input) {
        alert(input);
    }
});



2015-10-26

Serving a HTML file from Node JS

We can serve static files from the HTTP servers that we create using Node.js.

we are going to take advantage of two modules to do that. Those are the "connect" module and the "serve-static" module. So "connect" is an extensible HTTP framework that allows me to plug in additional functionality into my HTTP server and "serve-static" is one of the plug-ins that we are going to use to help us serve static files easily from our applications.

Step 1: Run "npm init"

Step 2 : Insert all required parameter in 'npm init' and create the start page 'index.js'.

Step 3: In package.json , add the dependencies as below and run 'npm install'

{
  "name": "demostaticfile",
  "version": "1.0.0",
  "description": "Service Static Content from web server",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "MRB",
  "license": "ISC",
  "dependencies" : {
    "connect"         : "*",
    "serve-static"    : "*"
   
  }
}


Step 4: Create a folder 'public' under the root and place html and image files within the public folder.

Step 5: Write below code in your index.js

var connect=require("connect");
var serveStatic=require("serve-static");

var app=connect()
  .use(serveStatic('public'))
  .use(function  (req,res) {
   res.end("Welcome to the Static Demo");
  })
  .listen(7878);

console.log("listening from port 7878");

Step 6: Open your browser and try browsing your test html file or image file

http://localhost:7878/test.html

2015-10-23

Creating a Node.js HTTP Client Application

This is useful when you are providing some sort of a proxy functionality or even some sort of web scraping type functionality where you are going to go out and grab web pages, and then attempt to process the HTML page, and get some information from it.

we are actually going to be using an external 'request' module.

Step 1: Run "npm init"

Step 2 : Insert all required parameter in 'npm init' and create the start page 'index.js'.

Step 3: In package.json , add the dependencies as below and run 'npm install'

"dependencies": {
"request": "*"
}


Package.json:

{
  "name": "demohttpclientapp",
  "version": "1.0.0",
  "description": "create a http client application",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "MRB",
  "license": "ISC",

  "dependencies": {
  "request": "*"
   }
}


Step 4:Write below code in your index.js

var request = require('request');
request('http://www.google.com', function(error, response, body){
if (!error && response.statusCode == 200){
console.log(body);
}
if (error){
console.log(error);
}
});

Step 5: Run the application by 'node index.js' and see the responses in console.

Configure responses from a Node.js web server

we can customize or configure the type of response that we send back in our application. And to help us in this demo, we are going to be using the "connect" module, which is a framework that's going to allow us to easily plug in functionality to our HTTP server.

Step 1: Run "npm init"

Step 2 : Insert all required parameter in 'npm init' and create the start page 'index.js'.

Step 3: In package.json , add the dependencies as below

"dependencies": {
"Connect": "*"
}


Step 4 :

var connect=require('connect');

var app=connect()
.use(function(req,res){


 if(req.url == "/hello")
 {
  console.log("sending plain");
  res.end("Hello from app");
 }

 else if(req.url == "/hello.json")
 {
  console.log("sending json");
  var data = "Hello";
  var jsonData = JSON.stringify(data);
  res.setHeader('Content-Type', 'application/json');
  res.end(jsonData);
    }

 else if(req.url == "/hello404")
 {
  console.log("sending 404 status code")
  res.statusCode = 404;
  res.end("Oops, could not find something");
 }

res.end("I am listening");

})
.listen(1111);

console.log("listening at port 1111");

Step 5: Run the application by 'node index.js' and try 'http://localhost:1111/hello' in browser

Managing HTTP request using Node JS

In this demo, we are going to look at how we can process a "POST" request that is going to come in with some form data within our Node application. And to do that, we are going to be using the help of the "connect" module and also the "body-parser" module.

Step 1: Run "npm init"

Step 2 : Insert all required parameter in 'npm init' and create the start page 'index.js'.

Step 3: In package.json , add the dependencies as below

"dependencies": {
"Connect": "*"
"body-parser":"*"
}


Step 4: in 'index.js', write the following code

var http=require('http');
var connect=require('connect');
var bodyParser=require('body-parser');

//bodyParser.urlencoded is going to make it easy for me to work with http form data by populating the body object of the http request that comes in, so I can //easily access that data.

var app=connect()
      .use(bodyParser.urlencoded(
        {extended:true}
       )
    )
      .use(function(req,res){
//Handler Method
var parsedInfo={};
parsedInfo.firstName=req.body.userFirstName;//
access the userFirstName and the userLastName properties of the body
parsedInfo.lastName=req.body.userLastName;

res.end("User Infor Parsed from: " +parsedInfo.firstName+" " + parsedInfo.lastName)
      }
);

 http.createServer(app).listen(3456);
 console.log("listening from 3456"); 

Step 5: Create a Html page with the following content


 

<html>
 <head>
 <title>User Infor From Page</title>
 </head>

 <body>
  <h2>Enter Detail and Submit</h2>
  <form action="http://localhost:3456" method="POST">
 First Name:<input Type="text" name="userFirstName"/>
 Last Name:<input Type="text" name="userLastName"/>
 <input type="submit" name="submit"/>
 </form>
 </body>
</html>

 User Info From Page
 
 
  

Enter Detail and Submit


  

 First Name:
 Last Name:
 
 

 

Step 6: run 'node index.js'

Step 7: Open the HTML page in browser, insert First & Last name and  submit the page.

Step 8:You will get the response in browser , as per the response object.

Creating an HTTP Server using Node JS

One of the purposes Node.js is commonly used for is creating HTTP servers capable of responding to client requests.

Step 1: Run "npm init"

Step 2 : Insert all required parameter in 'npm init' and create the start page 'index.js'.

Step 3: Create Http Server using 'createServer' method of 'http' module

var http=require('http');

var handlerMethod=function(req,res){
 res.end("Hello, This is a message from the handler of web server");
}

http.createServer(handlerMethod).listen(1234,'localhost');

console.log("Http Server is running");

Step 4: Run it using 'node index.js'

Step 5: open your brower and browse 'http://localhost:1234' to see the output.

It will display the message 'Hello, This is a message from the handler of web server' ,as mentioned in your response object.

2015-10-16

Node JS Modules

What is Module in Node JS

Modules make it possible to include other Javascript files into your applications. In fact, a vast majority of Node’s core functionality is implemented using modules written in JavaScript.

Using modules is simple: you use the require() function, which takes one argument: the name of a core library or a file system path to the module you want to load.

To make a module yourself, you need to specify what objects you want to export. The exports object is available in the top level scope in Node for this purpose.

Example, create hello.js and write the following code
 
exports.firstname='Manab';
exports.myfunction = function()
{  
return 'Hello World';
 
};
 
Then, in your index.js(calling Page),execute the module as below,
 
var hello = require('./hello.js');
console.log(hello.myfunction ()); // Print "Hello World"

 
But we also have the option of using the 'module.exports' to export just one single entity from our modules.

 

in hello.js

module.exports = function(){

//return Math.random();

return 'Hello World';
} 

 
In index.js
console.log(hello); // Print "Hello World"
 

Core modules :


Core modules that are provided by Node itself, examples are
 
1)var os = require ('os');
console.log(os);
console.log(os.platform());

2)var http = require('http');
console.log(http);

NPM Package Management  :

1)run 'npm init'.
2)Enter name, Version, Description and author and leave blank for other option to be defaulted.
3)It will generate package.json.
4)Manually create index.js as a default entry point
5)run 'npm install express --save'

The dependencies(in package.json) code block is as follows:
"dependencies": {
"express": "^4.9.6"
}



==>install 'express' which is getter than 4.9.6

6)run 'npm update' ==> after changing version of dependencies from package.json
7)run 'npm prune'  ==> after removing dependencies from package.json

 

NPM Modules :

This is the community modules find in the below location
https://www.npmjs.com/

Install the NMP modules after npm init and get a folder structure:

1)npm install underscore --save

var _ = require('underscore');
var names = ["Steve", "Stuart", "Ari", "Ayan"];
_.each(name, function(name){
console.log("Name is: " + name);
});


2)npm install    => if package.json(will get after npm init) contains the dependencies
 

Create Module(./)

Create fruit.js as below :

module.exports = function(name, description){
var name = name;
var description = description;
var functions = {
setName: function(nameIn)
{
this.description = description;
},
getInfo: function()
{
return{
name: name;
description: description;
}
}
};
return functions;
}



In Index.js

var fruit = require('./fruit.js');

var banana = fruit('banana', 'yellow fruit');
var cherry = fruit('cherry', 'small red fruit');
console.log(banana.getInfo());
console.log(cherry.getInfo());


 
 

Managing Node Version and Editor

Switching between different version of NodeJS:

Sometimes when we create our Node applications, it's going to be necessary to check or test our apps between different versions of Node just to make sure that everything is still working as expected.

Now there are a few tools that we can use to help us do this. If you are in a Linux based environment, you may use a tool such as NVM so that's a Node module. We are in a Windows based environment, so we are going to be using another Node module called NVMW as below.

[To test That, Create app.js and type console.log(process.version)  and execute it using 'node app.js' , before and after changing the current version]

C:\Manab>npm install -g nvmw

C:\Manab>nvmw install v0.10.26

C:\Manab>nvmw use v0.10.26

When I use nvmw in this command prompt, in the shell, it really is only valid for this shell.

To deactivate the older version in the current command prompt , just type below command :

C:\Manab>nvmw deactivate

Node JS Editor :

The Mostly used Editor is Sublime Text, could be downloaded from below location

http://www.sublimetext.com/

You could use , Node js Tools Visual Studio(NTVS) also as a NodeJS IDE



NODE JS and MEAN Stack

What is Mean :
MEAN is an opinionated fullstack javascript framework - which simplifies and accelerates web application development.Mean Stands for MongoDB,Express,NojeJS and AngularJS.

What is MongoDB :
MongoDB is the leading NoSQL database, empowering businesses to be more agile and scalable.

What is Express :
Express is a minimal and flexible node.js web application framework, providing a robust set of features for building single and multi-page, and hybrid web applications.

What is Node JS :
Node.js is a platform built on Chrome's JavaScript runtime for easily building fast, scalable network applications.It is an application development platform that allows us to create standalone applications developed using JavaScript.

What is AngularJS :
AngularJS lets you extend HTML vocabulary for your application. The resulting environment is extraordinarily expressive, readable, and quick to develop.

Install NodeJS in windows:
Go to the https://nodejs.org/en/ and Click 'Download for Windows'. Once download completed, install it.

In odder to check the installation, Open the command prompt and type node -v , it will show you the current version of node js installed at your PC.

C:\>node -v

REPL :
In order to start the Read-Eval-Print-Loop or REPL for short, I am going to just type the node command and I'm not going to use any of the flags. And you can see now my cursor has changed here to the > sign, and now I am actually in the REPL. And at this point, I'm just basically free to start entering lines of JavaScript that are going to be evaluated right away.

I can say something like console.log("Hello World") and press Enter.

Also , the multiline REPL command executed be as below in while loop

When you have done, press Ctrl+c twice or type process.exit(1) , in oder to exit the REPL.


  • Read - Reads user's input, parse the input into JavaScript data-structure and stores in memory.
  • Eval - Takes and evaluates the data structure
  • Print - Prints the result
  • Loop - Loops the above command until user press ctrl-c twice

  •