What is node.js

Node.js is a platform built on Chrome’s JavaScript runtime for easily building fast, scalable network applications. Node.js uses an event-driven, non-blocking I/O model that makes it lightweight and efficient, perfect for data-intensive real-time applications that run across distributed devices.”

In simple words,nodejs is a non-blocking code(anonymous function call/callback),event loop,asynchronous calling and single threaded server side framework.

23-05-2014 14-29-11

23-05-2014 14-37-05

23-05-2014 14-37-53

23-05-2014 14-40-42

Hmmm, okay that clears up the picture a little bit. Node.js is a platform that uses Chrome’s JavaScript runtime; that is a reference to Google’s open source V8 JavaScript engine.

Realization #1: Node.js is a framework that requires Google’s JavaScript engine.

v8 javascript engineQuestion: Isn’t JavaScript a browser side capability? Are we talking about building fast, scalable applications for a web browser? Building scalable applications for web browsers doesn’t make any sense to me.

In my search for answers I came across this great post on Mozilla.orghttps://hacks.mozilla.org/2012/01/javascript-on-the-server-growing-the-node-js-community/

The Mozilla post explains that JavaScript was originally intended to be both a browser AND server side technology. It also explains that Node.js is a server side implementation of JavaScript and is also single threaded. Even more interesting.

Node.js is a packaged compilation of Google’s V8 JavaScript engine, the libuv platform abstraction layer, and a core library, which is itself primarily written in JavaScript.” Beyond that, it’s worth noting that Ryan Dahl, the creator of Node.js, was aiming to create real-time websites with push capability, “inspired by applications like Gmail”. In Node.js, he gave developers a tool for working in the non-blocking, event-driven I/O paradigm.\

How Does It Work?

The main idea of Node.js: use non-blocking, event-driven I/O to remain lightweight and efficient in the face of data-intensive real-time applications that run across distributed devices.Node.js operates on a single-thread, using non-blocking I/O calls, allowing it to support support tens of thousands of concurrent connections.

toptal-blog-1_B

There is, of course, the question of sharing a single thread between all clients requests, and it is a potential pitfall of writing Node.js applications. Firstly, heavy computation could choke up Node’s single thread and cause problems for all clients (more on this later) as incoming requests would be blocked until said computation was completed. Secondly, developers need to be really careful not to allow an exception bubbling up to the core (topmost) Node.js event loop, which will cause the Node.js instance to terminate (effectively crashing the program).

The technique used to avoid exceptions bubbling up to the surface is passing errors back to the caller as callback parameters (instead of throwing them, like in other environments).

NPM: The Node Package Manager

When discussing Node.js, one thing that definitely should not be omitted is built-in support for package management using the NPM tool that comes by default with every Node.js installation. The idea of NPM modules is quite similar to that of Ruby Gems: a set of publicly available, reusable components, available through easy installation via an online repository, with version and dependency management.

A full list of packaged modules can be found on the NPM website https://npmjs.org/ , or accessed using the NPM CLI tool that automatically gets installed with Node.js.

 A brief introduction to NPM (a bit old, but still valid) can be found at http://howtonode.org/introduction-to-npm.

Some of the most popular NPM modules today are:

  • express – Express.js, a Sinatra-inspired web development framework for Node.js, and the de-facto standard for the majority of Node.js applications out there today.
  • connect – Connect is an extensible HTTP server framework for Node.js, providing a collection of high performance “plugins” known as middleware; serves as a base foundation for Express.
  • socket.io and sockjs – Server-side component of the two most common websockets components out there today.
  • Jade – One of the popular templating engines, inspired by HAML, a default in Express.js.
  • mongo and mongojs – MongoDB wrappers to provide the API for MongoDB object databases in Node.js.
  • redis – Redis client library.
  • coffee-script – CoffeeScript compiler that allows developers to write their Node.js programs using Coffee.
  • underscore (lodashlazy) – The most popular utility library in JavaScript, packaged to be used with Node.js, as well as its two counterparts, which promise better performance by taking a slightly different implementation approach.
  • forever – Probably the most common utility for ensuring that a given node script runs continuously. Keeps your Node.js process up in production in the face of any unexpected failures.

The list goes on. There are tons of really useful packages out there, available to all.

CHAT

Chat is the most typical real-time, multi-user application. From IRC (back in the day), through many proprietary and open protocols running on non-standard ports, to the ability to implement everything today in Node.js with websockets running over the standard port 80.

In the simplest example, we have a single chatroom on our website where people come and can exchange messages in one-to-many (actually all) fashion. For instance, say we have three people on the website all connected to our message board.

On the server-side, we have a simple Express.js application which implements two things: 1) a GET ‘/’ request handler which serves the webpage containing both a message board and a ‘Send’ button to initialize new message input, and 2) a websockets server that listens for new messages emitted by websocket clients.

On the client-side, we have an HTML page with a couple of handlers set up, one for the ‘Send’ button click event, which picks up the input message and sends it down the websocket, and another that listens for new incoming messages on the websockets client (i.e., messages sent by other users, which the server now wants the client to display).

When one of the clients posts a message, here’s what happens:

  1. Browser catches the ‘Send’ button click through a JavaScript handler, picks up the value from the input field (i.e., the message text), and emits a websocket message using the websocket client connected to our server (initialized on web page initialization).
  2. Server-side component of the websocket connection receives the message and forwards it to all other connected clients using the broadcast method.
  3. All clients receive the new message as a push message via a websockets client-side component running within the web page. They then pick up the message content and update the web page in-place by appending the new message to the board.toptal-blog-2_B

Where Node.js Can Be Used

SERVER-SIDE WEB APPLICATIONS

Node.js with Express.js can also be used to create classic web applications on the server-side. However, while possible, this request-response paradigm in which Node.js would be carrying around rendered HTML is not the most typical use-case. There are arguments to be made for and against this approach. Here are some facts to consider:

Pros:

  • If your application doesn’t have any CPU intensive computation, you can build it in Javascript top-to-bottom, even down to the database level if you use JSON storage Object DB like MongoDB. This eases development (including hiring) significantly.
  • Crawlers receive a fully-rendered HTML response, which is far more SEO-friendly than, say, a Single Page Application or a websockets app run on top of Node.js.

Cons:

  • Any CPU intensive computation will block Node.js responsiveness, so a threaded platform is a better approach. Alternatively, you could try scaling out the computation [*].
  • Using Node.js with a relational database is still quite a pain (see below for more detail). Do yourself a favour and pick up any other environment like Rails, Django, or ASP.Net MVC if you’re trying to perform relational operations.

For Manual and documentation, visit here

For slides clickhere

You can start learning here

The best framework to start with combination of Angular.js,node.js,Express and Mongo DB is MEAN

How to get start with is explained here

How to install nodejs??

NodeJS onto your workstation to run AppBuilder:
1. Download the NodeJS installer for your system from http://nodejs.org/download/.
2. Run the NodeJS installer, and follow the prompts in the Install Wizard.
3. Verify that the node.js folder is in your System Environment PATH.
4. Open a command prompt, and enter npm to verify the NodeJS package manager is
installed properly.

Hear the words of node.js by the creator RyanDahl here

According to him a node is simple element/object that acts like a connecting point. He explains very clearly in his words. Kindly watch it.

The best amazing work of node is shown below. Just type node in cmd then hit enter and type  1 + 2 will give the result. Similarly you can write your own functions. Its really awesome right 😉

Try..

process.pid

process – entire process

process.en – environment

Use control+c to come out of loop while infinite executing in nodejs

24-05-2014 19-18-59

 

Reference:

http://www.toptal.com/nodejs/why-the-hell-would-i-use-node-js

http://cwbuecheler.com/web/tutorials/2013/node-express-mongo/

http://code.tutsplus.com/tutorials/nodejs-for-beginners–net-26314

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s