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