See, problem solved. One language to rule them all. But this also brings you down to the fact that now your whole application is using the same language. That means you must have a sound knowledge of that language.
Here are the four bare minimum concepts that you should be able to put in practice to use Node.js effectively.
1. Non-blocking or Asynchronous I/O
Since Node.js is a server side framework, one of its main operations is to handle browser requests. In traditional I/O systems, a request can only be issued when the response (the HTML page) of the previous request has arrived. The server blocks other requests, called blocking I/O, in order to process the current request, which causes the browser to wait (the rotating circle).
Node.js doesn't follow this principle of I/O. If a request is intended to take longer, Node sends that request in an event loop (which I'll explain in greater depth in a different article) and goes on to handle the next request in the call stack. As soon as the pending request is done processing, it tells Node.js and the response is rendered on the browser.
Let’s understand this with a dummy example:
In this example of a restaurant, the waiter takes an order, waits for the order to complete, and then goes back to the table to serve the order each time. During the time the order is processing, the waiter just waits or blocks orders from other customers.
Non blocking I/O
In this example, the waiter takes an order, informs the cook of that order, and goes back to take another order. After the first order is done processing, the waiter takes that order to the respective table and goes on again to take orders from other customers. In contrast with the first example, the waiter doesn't block orders from other customers.
In a language that implements classical inheritance like Java or C++, for the purpose of code reuse, you first make a class (a blueprint for your objects) and then you create objects from that class or extend that class to inherit its properties.
Let’s illustrate that with the following sample code:
When you make a new object, you can select an object that should be its prototype. Here, we are adding a create method to the Object function. The create method creates a new object that uses another object as its prototype by passing the other object as an argument to the create method. When we make changes to the new object, its prototype remains unaffected. But when we make changes to the prototype object, the changes becomes visible in all the objects that are based on that prototype. Prototype is a complex concept. I'll be discussing it in detail in another post.
- Core Modules – These are the ones that come pre-compiled with the Node.js library. The purpose of core modules is to provide developers with often occurring and repeating code sections that, if they were not available, would result in a tedious task for developers because they have to write the same code again and again. Some common core modules are HTTP, URL, EVENTS, FILE SYSTEM, etc.
- User Defined Modules – User defined modules are the ones which a developer makes for a specific purpose in his/her application. These are required when the core modules are not capable of fulfilling the desired functionality.
Modules are extracted via the require function. If it’s a core module, the argument is simply the name of that module. If it's a user defined module, then the argument is the path of that module in the file system. For example,
When we pass a callback function as an argument to another function, we only pass the function definition, i.e., we never know when that callback function will execute. The timing of the execution solely depends on the mechanism of the calling function. It is “called back” at some later point of time, hence the name. That’s the sole basis of the non-blocking or asynchronous behavior of Node.js as illustrated by the following example.
This is one of the simplest example of a callback. We passed an anonymous function as an argument, which simply logs some output on the console to the setTimeout function. Since it's only the function definition, the function doesn't know when to execute. The execution time is determined by the calling setTimeout function via the second argument, which determines that it will be executed after 2 seconds. First, the second log statement logs the output to the console and then after two seconds, the log statement in the callback function logs the output.
What is in your list? Please tell me in the comments.
If you have the skills but you just aren't seeing the success you deserve in your career, then do check out our Simple Programmer course “How to Market Yourself as a Software Developer”.