An Introduction to

{ By: "Nirmal V",
Date: 19/07/2015
Location: Kochi Ruby Users Group, Kochi }

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.

What is Node.js

Concept of
Parallel Downloading

XHR Eval – Download the script via XHR and eval() the responseText.
XHR Injection – Download the script via XHR and inject it into the page by creating a script element and setting its text property to the responseText.

Script in Iframe – Wrap your script in an HTML page and download it as an iframe.
Script DOM Element – Create a script element and set its src property to the script’s URL.
For Example,
 var script = document.createElement("script");
script.type = "text/javascript";
script.src = "foo.js";
document.getElementsByTagName("head")[0].appendChild(script);

Script Defer – Add the script tag’s defer attribute. This HTML5 feature now supports by all major web browsers
document.write Script Tag – Write the <script src=""> HTML into the page using document.write. This only loads script without blocking in IE.

6 main techniques for downloading scripts without blocking:

var xhr = new XMLHttpRequest();
xhr.open("get", "file1.js", true);
xhr.onreadystatechange = function(){
if (xhr.readyState == 4){
if (xhr.status >= 200 && xhr.status < 300 || xhr.status == 304){
var script = document.createElement("script");
script.type = "text/javascript";
script.text = xhr.responseText;
document.body.appendChild(script);
}
}
};
xhr.send(null);

XHR Injection Example

Synchronous vs Asynchronous Programming

//example 1
var result = database.query("SELECT * FROM hugetable");
console.log("query finished");
console.log("Next line");


//example 2
database.query("SELECT * FROM hugetable", function(rows) {
    console.log("query finished");
});
console.log("Next line");
Made with Slides.com