Sunday, September 28, 2014

Getting started with Node.js

Introduction


In this module, we will introduce you to node.js, its features, advantages and its supports.

Overview
Node.js is a cross-platform runtime environment for server-side and networking applications. Node.js applications are written in JavaScript, and can be run within the Node.js runtime on OS X, Microsoft Windows and Linux with no changes.
Node.js applications are designed to maximize throughput and efficiency, using non-blocking I/O and asynchronous events. It is commonly used for real-time applications due to its asynchronous nature. Node.js internally uses the Google V8 JavaScript engine to execute code, and a large percentage of the basic modules are written in JavaScript. Node.js contains a built-in asynchronous I/O library for file, socket, and HTTP communication, which allows applications to act as a Web server without software such as Apache HTTP Server or IIS.
The goal of Node.js is to offer an easy and safe way to build high performance and scalable network applications in JavaScript. Node.js is part of the Server Side JavaScript environment and extends the JavaScript API to offer server side features. Node.js is able to do this by handling many client connections at the same time. The Node.js API can be extended using the CommonJS module system.

Threading
Traditional Web-serving techniques require each connection (request) to create a new thread, taking up system RAM and eventually maxing-out at the amount of RAM available. Node.js is different. It operates on a single-thread, using non-blocking I/O calls, allowing it to support tens of thousands of concurrent connections, without worrying about RAM limitations and the cost of context-switching between threads. The design of sharing a single-thread between all the requests means it can be used to build highly concurrent applications. The design goal of a Node.js application is that any function performing an I/O must use a callback

Package management
Npm is the package manager that comes pre-installed with the Node.js server platform. It is used to install Node.js programs from the npm registry. By organizing the installation and management of third-party Node.js programs, it helps developers build faster. npm is not to be confused with the commonJS require() statement. It is not used to load code: instead, it is used to install code and manage code dependencies from the command line. The packages found in the npm registry can range from simple helper libraries like underscore.js to task runners like grunt.js.

Unified API
Node.js combined with a browser, a document DB (such as MongoDB or CouchDB) and JSON offers a unified JavaScript development stack. With the increased attention to client-side frameworks and the adaptation of what were essentially server-side development patterns like MVC, MVP, MVVM, etc., Node.js allows the reuse of the same model and service interface between client-side and server-side.

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.
How it works under-the-hood is pretty interesting. Compared to traditional web-serving techniques where each connection (request) spawns a new thread, taking up system RAM and eventually maxing-out at the amount of RAM available, Node.js operates on a single-thread, using non-blocking I/O calls, allowing it to support tens of thousands of concurrent connections.

Tools


Desktop IDEs
Online code editors
Runtimes and debuggers

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. The module ecosystem is open to all, and anyone can publish their own module that will be listed in the NPM repository. 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.

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.

Where Node.js should not be used?


Server-side web applications w/a relational db behind
Comparing Node.js with Express.js against Ruby on Rails, for example, there is a clean decision in favour of the latter when it comes to relational data access. Relational DB tools for Node.js are still in their early stages; they’re rather immature and not as pleasant to work with. On the other hand, Rails automagically provides data access setup right out of the box together with DB schema migrations support tools and other Gems (pun intended). Rails and its peer frameworks have mature and proven Active Record or Data Mapper data access layer implementations, which you’ll sorely miss if you try to replicate them in pure JavaScript. Still, if you’re really inclined to remain JS all-the-way (and ready to pull out some of your hair), keep an eye on Sequelize and Node ORM2—both are still immature, but they may eventually catch up.

Conclusion


We've discussed Node.js from theory to practice, beginning with its goals and ambitions, and ending with its sweet spots and pitfalls. When people run into problems with Node, it almost always boils down to the fact that blocking operations are the root of all evil—99% of Node misuses come as a direct consequence. Remember: Node.js was never created to solve the compute scaling problem. It was created to solve the I/O scaling problem, which it does really well. Why use Node.js? If your use case does not contain CPU intensive operations nor access any blocking resources, you can exploit the benefits of Node.js and enjoy fast and scalable network applications. Welcome to the real-time web.


In next modules, we will be having a look for how to use it in our applications.

Getting started with MongoDb

Introduction


In this module, we will introduce you to mongodb, its features, advantages and why do we need it.

Overview
MongoDB is classified as a NoSQL database written in c++ and it is a cross-platform, document oriented database that provides, high performance, high availability and easy scalability. MongoDB works on concept of collection and document.

Database
Database is a physical container for collections. Each database gets its own set of files on the file system. A single MongoDB server typically has multiple databases.

Collection
Collection is a group of MongoDB documents. It is the equivalent of an RDBMS table. A collection exists within a single database. Collections do not enforce a schema. Documents within a collection can have different fields. Typically, all documents in a collection are of similar or related purpose.



Document
A document is a set of key-value pairs. Documents have dynamic schema. Dynamic schema means that documents in the same collection do not need to have the same set of fields or structure, and common fields in a collection's documents may hold different types of data.



RDBMS terminology with MongoDB


Licensing and support

MongoDB is available for free under the GNU Affero General Public License. The language drivers are available under an Apache License. In addition, MongoDB Inc. offers commercial licenses for MongoDB.

Downloads


To install the MongoDB on your OS, first download the latest release of MongoDB from http://www.mongodb.org/downloads Make sure you get correct version of MongoDB depending upon your OS version. Please follow my installation module to get installation process.

Advantages

Any relational database has a typical schema design that shows number of tables and the relationship between these tables. While in MongoDB there is no concept of relationship

Advantages of MongoDB over RDBMS
  • Schema less : MongoDB is document database in which one collection holds different different documents. 
    Number of fields, content and size of the document can be differ from one document to another.
  • Structure of a single object is clear
  • No complex joins
  • Deep query-ability. MongoDB supports dynamic queries on documents using a document-based query language 
    that's nearly as powerful as SQL
  • Tuning
  • Ease of scale-out: MongoDB is easy to scale
  • Conversion / mapping of application objects to database objects not needed
  • Uses internal memory for storing the (windowed) working set, enabling faster access of data

Features

Some of the main features include:
Document-oriented
Instead of taking a business subject and breaking it up into multiple relational structures, MongoDB can store the business subject in the minimal number of documents. For example, instead of storing title and author information in two distinct relational structures, title, author, and other title-related information can all be stored in a single document called Book, which is much more intuitive and usually easier to work with.
Ad hoc queries
MongoDB supports search by field, range queries, regular expression searches. Queries can return specific fields of documents and also include user-defined JavaScript functions.
Indexing
Any field in a MongoDB document can be indexed (indices in MongoDB are conceptually similar to those in RDBMSes). Secondary indices are also available.
Replication
MongoDB provides high availability with replica sets. A replica set consists of two or more copies of the data. Each replica set member may act in the role of primary or secondary replica at any time. The primary replica performs all writes and reads by default. Secondary replicas maintain a copy of the data on the primary using built-in replication. When a primary replica fails, the replica set automatically conducts an election process to determine which secondary should become the primary. Secondaries can also perform read operations, but the data is eventually consistent by default.
Load balancing
MongoDB scales horizontally using sharding. The user chooses a shard key, which determines how the data in a collection will be distributed. The data is split into ranges (based on the shard key) and distributed across multiple shards. (A shard is a master with one or more slaves.)
MongoDB can run over multiple servers, balancing the load and/or duplicating data to keep the system up and running in case of hardware failure. Automatic configuration is easy to deploy, and new machines can be added to a running database.
File storage
MongoDB can be used as a file system, taking advantage of load balancing and data replication features over multiple machines for storing files.
This function, called GridFS, is included with MongoDB drivers and available with no difficulty for development languages . MongoDB exposes functions for file manipulation and content to developers. GridFS is used, for example, in plugins for NGINX and lighttpd. Instead of storing a file in a single document, GridFS divides a file into parts, or chunks, and stores each of those chunks as a separate document.
In a multi-machine MongoDB system, files can be distributed and copied multiple times between machines transparently, thus effectively creating a load-balanced and fault-tolerant system.
Aggregation
MapReduce can be used for batch processing of data and aggregation operations. The aggregation framework enables users to obtain the kind of results for which the SQLGROUP BY clause is used.
Server-side JavaScript execution
JavaScript can be used in queries, aggregation functions (such as MapReduce), and sent directly to the database to be executed.
Capped collections
MongoDB supports fixed-size collections called capped collections. This type of collection maintains insertion order and, once the specified size has been reached, behaves like acircular queue.

Where should we use MongoDB?

Mongodb features:
  • Big Data
  • Content Management and Delivery
  • Mobile and Social Infrastructure
  • User Data Management
  • Data Hub

In next modules, we will be having a look for how to use it in our applications for CRUD operations.


MongoDB: quick installation guide

Introduction


In this module, we will be demonstrating how to install mongodb for various OS.

Environment


1. Install MongoDB On Windows

To install the MongoDB on windows, first doownload the latest release of MongoDB from http://www.mongodb.org/downloads. Make sure you get correct version of MongoDB depending upon your windows version. To get your windows version open command prompt and execute following command.

32-bit versions of MongoDB only support databases smaller than 2GB and suitable only for testing and evaluation purposes.
Now extract your downloaded file to c:\ drive or any other location. Make sure name of the extracted folder is mongodb-win32-i386-[version] or mongodb-win32-x86_64-[version]. Here [version] is the version of MongoDB download.
Now open command prompt and run the following command


In case you have extracted the mondodb at different location, then go to that path by using command cd folder/dir and now run the above given process.
MongoDB requires a data folder to store its files. The default location for the MongoDB data directory is c:\data\db. So you need to create this folder using the Command Prompt. Execute the following command sequence


If you have install the MongoDB at different location, then you need to specify any alternate path for \data\db by setting the path dbpath in mongod.exe. For the same issue following commands. In command prompt navigate to the bin directory present into the mongodb installation folder. Suppose my installation folder is D:\set up\mongodb.


This will show waiting for connections message on the console output indicates that the mongod.exe process is running successfully.
Now to run the mongodb you need to open another command prompt and issue the following command


This will show that mongodb is installed and run successfully. Next time when you run mongodb you need to issue only commands


2. Install MongoDB on Ubuntu

Run the following command to import the MongoDB public GPG Key:


Create a /etc/apt/sources.list.d/mongodb.list file using the following command.


Now issue the following command to update the repository:


Now install the MongoDB by using following command:


In the above installation 2.2.3 is currently released mongodb version. Make sure to install latest version always. Now mongodb is installed successfully.

Start MongoDB


Stop MongoDB


Restart MongoDB


To use mongodb run the following command


This will connect you to running mongod instance.

Help

To get list of commands type db.help() in mongodb client, this will give you list of commands as follows:


Statistics

To get stats about mongodb server type the command db.stats() in mongodb client, this will show the database
name, cumber of collection and documents in the database. Output the command is shown below: