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.
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.
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 :
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 :
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”
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.
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