Skip to main content

mynodejs

Event-driven programming can be overwhelming for beginners, which can makeNode.js difficult to get started with. But don't let that discourage you; In this article, I will teach you some of the basics of Node.js and explain why it has become so popular.
Node.js uses a module architecture to simplify the creation of complex applications.
Chances are good that you are familiar with asynchronous programming; it is, after all, the "A" in Ajax. Every function in Node.js is asynchronous. Therefore, everything that would normally block the thread is instead executed in the background. This is the most important thing to remember about Node.js. For example, if you are reading a file on the file system, you have to specify a callback function that is executed when the read operation has completed.
Node.js is only an environment - meaning that you have to do everything yourself. There is not a default HTTP server, or any server for that matter. This can be overwhelming for new users, but the payoff is a high performing web app. One script handles all communication with the clients. This considerably reduces the number of resources used by the application. For example, here is the code for a simpleNode.js application:
var i, a, b, c, max;

max = 1000000000;

var d = Date.now();

for (i = 0; i < max; i++) {
    a = 1234 + 5678 + i;
    b = 1234 * 5678 + i;
    c = 1234 / 2 + i;
}

console.log(Date.now() - d);
And here is the equivalent written in PHP:
Now let's look at the benchmark numbers. The following table lists the response times, in milliseconds, for these two simple applications:
Number of iterationsNode.jsPHP
1002.000.14
10'0003.0010.53
1'000'00015.001119.24
10'000'000143.0010621.46
1'000'000'00011118.001036272.19
I executed the two apps from the command line so that no server would delay the apps' execution. I ran each test ten times and averaged the results. PHP is notably faster with a smaller amount of iterations, but that advantage quickly dissolves as the number of iterations increases. When all is said and done, PHP is 93% slower than Node.js!
Node.js is fast, but you will need to learn a few things in order to use it properly.
Node.js uses a module architecture to simplify the creation of complex applications. Modules are akin to libraries in C, or units in Pascal. Each module contains a set of functions related to the "subject" of the module. For example, the http module contains functions specific to HTTP. Node.js provides a few core modules out of the box to help you access files on the file system, create HTTP and TCP/UDP servers, and perform other useful functions.
Including a module is easy; simply call the require() function, like this:
var http = require('http');
Node.js is only an environment; you have to do everything yourself.
The require() function returns the reference to the specified module. In the case of this code, a reference to the http module is stored in the http variable.
In the above code, we passed the name of a module to the require() function. This causes Node to search for a node_modules folder in our application's directory, and search for the http module in that folder. If Node does not find the node_modulesfolder (or the http module within it), it then looks through the global module cache. You can also specify an actual file by passing a relative or absolute path, like so:
var myModule = require('./myModule.js');
Modules are encapsulated pieces of code. The code within a module is mostly private - meaning that the functions and variables defined within them are only accessible from the inside of the module. You can, however, expose functions and/or variables to be used outside of the module. To do so, use the exports object and populate its properties and methods with the pieces of code that you want to expose. Consider the following module as an example:
var PI = Math.PI;

exports.area = function (r) {
  return PI * r * r;
};

exports.circumference = function (r) {
  return 2 * PI * r;
};
This code creates a PI variable that can only be accessed by code contained within the module; it is not accessible outside of the module. Next, two functions are created on the exports object. These functions are accessible outside of the module because they are defined on the exports object. As a result, PI is completely protected from outside interference. Therefore, you can rest assured thatarea() and circumference() will always behave as they should (as long as a value is supplied for the r parameter).
Node is a JavaScript environment running in Google's V8 JavaScript engine. As such, we should follow the best practices that we use for client-side development. For example, we should avoid putting anything into the global scope. That, however, is not always possible. The global scope in Node is GLOBAL (as opposed to windowin the browser), and you can easily create a global variable of function by omitting the var keyword, like this:
globalVariable = 1;
globalFunction = function () { ... };
Once again, globals should be avoided whenever possible. So be careful and remember to use var when declaring a variable.
Naturally, we need to install Node before we can write and execute an app. Installation is straight forward, if you use Windows or OS X; the nodejs.org website offers installers for those operating systems. For Linux, use any package manager. Open up your terminal and type:
sudo apt-get update
sudo apt-get install node
or:
sudo aptitude update
sudo aptitude install node
Node.js is in sid repositories; you may need to add them to your sources list:
sudo echo deb http://ftp.us.debian.org/debian/ sid main > /etc/apt/sources.list.d/sid.list
But be aware that installing sid packages on older systems may break your system. Be careful, and remove /etc/apt/sources.list.d/sid.list after you finish installing Node.
also for practices:
www.nodeschool.com

Comments

Post a Comment

Popular posts from this blog

Learn Nodejs

My favorite resource is "nodeschool.io!" "Install these choose-your-own-adventure style lessons and learn how to use node.js, npm and other related tools by writing code to solve realistic problems. The lessons run in your terminal and work on Windows, Mac and Linux."   Tutorials Hello World Hello World Web Server Node.js guide Build a blog with Node.js, express and mongodb Node.Js Tutorials At Project 70 Node.js for Beginners Learn Node.js Completely and with Confidence Videos Node tuts Introduction to Node.js with Ryan Dahl Node.js: Asynchronous Purity Leads to Faster Development Parallel Programming with Node.js Server-side JavaScript with Node, Connect & Express Node.js First Look Node.js with MongoDB Ryan Dahl's Google Tech Talk Screencasts Learn All The Nodes NodeTuts NodeCasts Books " The Node Beginner Book Mastering Node.js Up and Running with Node.js Node.js in Action Smashing Node.js: JavaScript Ev...

Integrating Elastic Search With Mongodb Using No-SQL

An insight about search engine: An application which provides the searched results of documents over the web for some itemized keywords. Here a series of document are found out which contains those specified keywords. Moreover, search engine is a pack of programs usually framed to be the Systems. More often than, Web Search engines perform its work by transmitting the data packets to retrieve the maxim amount of docs. Then an Indexer goes through these documents and formulates an Index depending on word count for every document. Several search engines use a varied algorithm to build Indices in such a way that uniquely worthy outcome is delivered to each & every query. Elastic search, Bobo Search, Index Tank, Apache Solr, Summa, Compass, Katta, Constellio ETC. are some of the search engines which are available in the Market. Also, every search engine has their own idiosyncrasy. An insight about Elastic search: Elastic Search is an immensely extensive Open Source sear...