Saturday 29 December 2012

Nodetime in node.js

-->
Nodetime is node js app performance management tool set. This tool is for monitor and tracing application performance.It reveals the response time. Nodetime also supports HttpClient, cluster, file system, socket.io, MongoDb, Redis, PostgreSQL.

How to install nodetime with npm


npm install nodetime -g


then in project we need to include


require('nodetime').profile()

The high level profiler ruuning in app will securely sends profiling data to nodetime server.

when you start application, a link containing url and session_id will be printed to console.


ex:
https://nodetime.com/[session_id]

 How to get session_id

var nodetime=require('nodetime');      // nodetime is the variable, and  we are implementing module nodetime.


nodetime.on('session',function(id)
{
}).profile();

Methods:-

profile - starts the profiler. it contains some options.

headless , dtrace, stdout, debug.
what are these options
headless - if it true, no data will sent to the server.
dtrace     - It activates firing of Dtrace probes.
stdout     - if true, dumps using console.log().
debug     - used for debugging nodetime.



pause()  - it deactivates the profiler.

resume() - it activates the profile

resume([seconds])- it activates profiler for  given duration.

filter(test)- sets the sample filter.


Thursday 27 December 2012

HTTP module





Node fit for http server. To use http module, client must require('http).

To create http server


http.createServer([requestListener])   - it will create a new webserver object.

http.request([port,host])  - refer to the server to connect.


see this simple example
///here we have to include the module, http is a variable, im taking

var http=require("http");


//the request is a read stream which gives data for incoming whereas response object is a write stream //   is used to send data to the client.

var server=http.createServer(function(req,res){




// here header we are sending  is 200
    res.writeHead(200,{'Content-Type': 'text/plain'});
    res.end('hello welcome to nodemongo.asia\n');
   
});
// to run the server on port


server.listen(1337,"localhost");


Node.js offers http module and createServer method returns an object to respond http requests. Object inherits http.Server prototype.



Wednesday 26 December 2012

Features of MongoDB

1. Document oriented storage

2. Auto Sharding

3. Querying

4. Full index support

5. Map

6. GridFS

Tuesday 25 December 2012

1st WebServer with node.js

In this article, i will create 1st Web Server.

STEPS:

Listen port
Receive Request
send response
loading static files


Http Module:


include http module

var http=require("http");

create a server

var server=http.createServer(function(request, response){


write some headers

response.writeHead(200,{"content-type":"text/plain"}};
response.end("hello");
}).listen(8000);


Features of Node.js

Features:
The main features of node.js as follows

Event-Driven
Non blocking I/O
Node Package Manager
Web Sockets




Even Driven:

Node.js is  an event driven model. Programming node.js has lots of call backs.It is single threaded. So, only one thing happens at once.

lets take an example,

Paper boy will deliver the papers regularly from starting nearest house.He will deliver the paper one by one home.If somebody asks for paper which is not delivered by the paper boy, he may deliver or not deliver. Calling the paper boy is like a callback. Similarly, in node.js also lots of call backs involved.



Non Blocking I/O:
Non blocking means other operations can be done while another operation is in progress.


Node Package Manager:

It contains collection of node modules. for more details see  NPM topic in the same blog.

Web Sockets:
Web sockets provides bidirectional communication over single TCP.
It is the standard way for server to send messages to client without client request.



these are the main features of node.js








Monday 24 December 2012

NPM

What is NPM?

NPM stands for Node Package Manager.
Whne we install node in our system, npm comes with node. It is a set of directory of modules, we can install individual modules depending on our requirement.

To install any module

in Git Bash
type the following command

npm install   modulename -g


or

npm install   modulename

what is the difference between these two.

1st one will install globally, where as 2nd one will install locally.  If we  install  the module globally, then no need to install module every project.If we install locally, we need to install the module for every project.

Update the module:

if the module gets updated, we need to update the module by using the following command
npm update modulename


Sunday 23 December 2012

Node.js Modules

Modules:

       In object oriented programming languages like java, we need to include jar files to get the particular functionality if the native jdk does not provide the same feature.
     In the similarly, Modules provides special features and functionalities.In Node.js we have three types of modules.

  1. Core Modules
  2. custom modules
  3. Third party modules.


These modules, we can install as per our project requirement

To install any core module,

Go to the path of the project,


 type the following command

npm install modulename  -g

here "g" refers to global. If we declare -g, the module will install globally.

Now we will see some basic modules,


File System module:

By using this module, we can read the data from the harddisk.To read data from the file, we need to include file system module.


var fs=require("fs");
console.log("starting  the file");
//fs.readFile(file location,function(error, context)  - for reading the file
//fs.writeFile(file location,function(error, context)  - for writing the file

fs.readFile("path of text file",function(err,data){
   
    console.log("contents in the file",+data);
   
});
console.log("executing in the file");


when we run the program, first it will print starting file. then we it has to load the file, if it ends, it has to call the function. It will print executing in the file while it waiting for the function to load the file.

Now let us see the nodejs.org,

go to FileSystem module,you can find lots of methods available for file system.
You can use any methods.


 
 Web Frameworks:


Routers
Static file servers
frameworks
microframeworks
Middleware



Routers:

connect-route: A simple router for connect.

How to install?

npm install connect-route -g


How to use:

See this example
var connect = require("connect");
var connectRoute = require("connect-route");

connect(
  connect.static(dirname + "/public"),
  connectRoute(function(app) {
    app.get("/hello", function(req, res) {
        html = "<!doctype html>" +
          "<html><head><title>Hello  MobEngineers</title></head>" +
          "<body><h1>Hello Mob Engineers</h1></body></html>";

      res.end(html);
    });
  })
).listen(8008);


Thursday 20 December 2012

MongoDB Introduction


Mango DB is an open source database developed in C++. It can be used to store data for high performance applications.It is released in february 2009



Great Features of MangoDb

Document base storage:

MongoDb is a document based database where as Oracle,mysql,sql server are relational databases.It stores whole data as BSON(Binary JSON) format.


Map:

  It supports map framework for batch processing of data. here map means, a master node takes an input, split it into smaller sections and send it to associated nodes. Here smaller nodes process the problem and it will send the processed data to master node.



Sharding:
  If load increases, it distributed to other nodes, this is called Sharding.

Wednesday 19 December 2012

Node.js Introduction


Node is a platform for writing server side javascript
applications uses asynchronous event driven model.
Every function in node.js is asynchronous.
Node is a platform means you have to do everything yourself.
One script handles all communication with clients.
Node cannot use for gui applications.
It allows javascipt to be execute on server side. It uses
the wicked fast V8 JavaScript engine which is developed by the Google for Crome
browser.
Node.js allows Javascript to be executed on the server side, and
it uses the wicked fast V8 Javascript engine which was developed by Google for
the Chrome browser
One of the disadvantage of thread based is need of more
memory. If we use threads, we can do multicore.
Node has a single execution thread with no waiting on I/O or
context switching.
In node js, we can easily handle 10k clients at same time in
parallel on one machine.