Getting Started – Node.js

Getting Started – 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.

Before start using Node.js, you must first understand the differences between Node.js and traditonal server-side scripting environment like PHP, Phyton, Ruby, etc.

 

Asynchronous Programming

 

Every functions in Node.js is asynchronous. Everything that would normally block the thread is instead executed in the background. 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.

 

Synchronous is a control which back to the user after the input and output process has done so user need time to wait if there are another process need to execute. Example: take a photo on webcam notebook, there’s a waiting process before continue to the next photo taking session.

Asynchronous is a control which back to user without waiting input and output process finished to execute. So there will not any wait loop. Example : when we are chatting, when we write a character, we don’t need to wait to type another characters.

 

Modules

 

Node.js uses module architecture to simplify the creation of complex applications. Modules are skin to libraries C, or unit in Pascal. Each module contains a set of functions related to “Subject”of the module. Example : http module contains functions specific to HTTP.

1. How to Include The Module :

 

[code language="javascript"]
	var http = require(“http”);
[/code]

 

The code above, we put the reference of http module into the variable called http. we passed the module name into the require() function. This function will search the node_modules folder in our application’s directory, and search for the http module in that folder. If node does not find the node_modules folder then it will look through the global module cache.

 

2. How to Include Another .js File :

 

[code language="javascript"]
	var myModule = require(“./myModule”);
	var myModule = require(“./myModule.js”);
[/code]

 

Modules are encapsulated pieces of codes. The function inside the module is private, means that the function is only accessible from the inside of the module. But, we can access that function from another module by using exports. Here is how to use exports : exports.start = startFunction; //startFunction is a function for example just for displaying text “Hello”

[code language="javascript"]
	var PI = Math.PI;
	exports.area = function (r) { return PI * r * r; };
	exports.circumference = function (r) { return 2 * PI * r; };
[/code]

 

Variable PI can not access from another module because it is just a private variable, but here we have 2 functions that can access from another module, those are area and circumference, they are exported.

 

Global Scope

 

Node js is a javascript environment running in Google 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. It however is not always possible. To create the global variable, we only need to omitting the var keyword :

[code language="javascript"]
	globalVariable = 1;
[/code]

 

Notes: once again, globals should be avoided whenever possible. So be careful and remember to use var when declaring a variable.

 

 

Written by : Team Bala-Bala at 41studio

%d bloggers like this: