Skillset

When I started working on NodeJS, I had no prior experience nor knowledge about NodeJS nor about Javascript. It was kind of a painful journey, considering the fact that I was migrating myself from a structured programming which deals with linear method of programming to something which is asynchronous.

Many may argue that by using threads we can achieve asynchronous behavior of the executed code, however when we really start looking into NodeJS as a noob, we come across various pitfalls. In order to explain this in a much better manner, let us view the codes written in C and NodeJS, both of which are doing the same task, ie., writing data into a file.

Source code of a program written in NodeJS. In this NodeJS code, I have added some console output code so as to understand the functional working of Asynchronous Coding. Keep a tab on the value of “i” and observe the changes as this code gets executed.

From the point of view of a programmer who has worked on structured languages, at first glance I would admit that both the codes should at the end of execution provide a file which contains the value of “i” and its square, while at the end of the execution the file should be written with “Completed Loop”, and just prior to the termination the value of “i” would be modified. However, after the actual execution, both the console outputs differ from each other, and moreover, based on the console output, we observe that the control of the execution in NodeJS is strikingly different than that of C, and also in between the execution the value of “i” has changed.

From the console output of NodeJS it is quite evident that even before the “for loop” can finish its logical execution, ie. writing of the file content, the next-in-line statement was executed, and along with this the value of “i” was changed. This effectively has affected the functional / logical aspect of the program.

Even though the values to be written in the file were unchanged, it simply demonstrates the fact that when dealing with asynchronous programming, the value of the variables are dependent on the manner in which the code gets executed and not where they are placed, as it is in the case with C or other languages. Also, the behavior of asynchronous functions is quite different when compared with the form of coding / code execution most of us are used to.

“NodeJS applications are designed to maximize throughput and efficiency, using non-blocking I/O and asynchronous events. NodeJS applications run single-threaded, although NodeJS uses multiple threads for file and network events.“

A brief look into the introduction of NodeJS is sure enough to make us realize that we indeed need to change our perspective while coding in NodeJS. Hence, those vying to switch over to NodeJS should bear in mind the intricacies of Asynchronous programming. The above mentioned issue is just one of the many which we may come across, in case we do not understand the basic concepts of NodeJS and the methods that need to be used to successfully write a piece of code / complete a project in NodeJS.

For beginners, one may come across lots of websites which help in making you understand NodeJS, however over here in this article we will be taking a look in some other aspects which are necessary for a beginner when they straightaway start working on NodeJS.

Before we proceed, let us explore some concepts related to:

1: Strings and Arrays
2: Callbacks

String and Arrays

A small piece of code will give you a simple walkthough of strings, arrays and objects.

The above example is self explanatory and shows most of the methods used for defining / manipulating strings, arrays and objects. While switching over to NodeJS, knowledge of JavaScript is a must – without it, you would lost in the Garden of Eden.

Traversing through an array can be accomplished either by way of a conventional “for()” loop or using “forEach()”. Moreover, String operations are similar to arrays, and to make the matters worse, a string can dynamically hold any data type, ie. number / string. Hence, while coding, maintain a strict naming convention and keep a tab on the data types of the values that are associated with a particular variable. These small tips shall help you tide over while trouble-shooting. Utilize “typeof()” to verify the data-type and also remember to use JSON.stringify() / JSON.parse() whenever you encounter an array or object. However the basic rules remain the same – Non-Blocking I/O and Asynchronous.

CallBacks

NodeJS uses callbacks to a great effect, and JavaScript programmers are well aware of its functioning. Also ,most of the functions use callbacks, and on numerous occasions you would realize that without callbacks some of the tasks are simply not possible.

Let us first take a look at the skeletal structure of the callback, and then we can use this to either write our own functions or utilize the callbacks rather more efficiently.

As an exercise, execute the above code with different values of num1/num2 and observe the various output scenarios. Note: The last parameter of a function is a callback function and it is defined as an argument.

For C programmers, this may come as a surprise, but this is the way callbacks are. In numerous languages the arguments of a function are always one of the data-types and never a function. However in JavaScript this is not the case.

When we take a look into live examples say for example a DNS client written purely in NodeJS “https://github.com/tjfontaine/node-dns” , the sample code for the client has been provided as is and it is our task to integrate this as a function within our code. We first define a function dns_ query(dns, util, domain_name, ns_ip, record_type ,DNS_cb) where DNS_cb is the callback function.

Compare the two codes and observe that wherever we wanted to receive the actual response we have used a callback, whereas the dns_query is called with a few parameters which includes the handle for “dns npm module & util npm module” and the callback function.

NodeJS has NPM modules which have been developed to provide various functionalities and are integrated within your code to make your life rather simple. Either you directly install the required NPM or define it in package.json.

The above mentioned code is just the basics, however as you start coding you will realize the Asynchronous Coding and Callbacks are creating a nightmare of all sorts. One needs to have control over flow the asynchronous aspect of NodeJS and help us in executing the code in a linear manner. There exists some excellent modules which will allow you to do just that. My personal favourite is the async by Caolan and can be found over here “https://github.com/caolan/async“.

The methods which you use for processing may differ in its method of execution and for which various Control-Flow methods have been provided.

1: Series: This is used when you want to execute the tasks one at a time.

2: Parallel: This is used when we have to execute all the tasks in parallel.

3: Waterfall: This is used when you need to pass on the results of each task into the next one.

Also when you iterate an array, wherein each and value of the array is processed in a series you may choose to use “eachseries” which is found under “Collections”. Roughly the term “Collections” translates to a data set; in this case it’s an array.

Also note that there are various methods for defining a control-flow function. Some may prefer to define an array containing the function code, yes, you heard it right, an array containing the code. Alternatively, you may directly write the function names / code within async.series(), however it would pose more of a problem while troubleshooting. Also it is interesting to note that the output is in the form of an array.

Our very first example was dealing with arrays. You may use async.series and async.parallel to understand the basic differences, moreover, when we are dealing with databases we need to use this module, as based on the output we have to decide the outcome of the code-execution.

Basic Setup

Enough said, we proceed with the installation and deployment of essential tools for coding in NodeJS.

Download and install the following:

1: NodeJS : http://nodejs.org/download/

2: Eclipse IDE : http://www.nodeclipse.org/updates/

Create a new project in Eclipse Studio and modify the package.json . This file contains the NPM you would want to use and app.js is the file which contains the code.

In order to ensure that all the requisite NPM modules are installed as defined in package.json, right click on package.json  Run As  npm install to install the NPM packages. Alternatively, you may also use the command line.

Those who would want to use the command line should, after installing NodeJS, create a project directory which holds the package.json and then issue the command

“npm install”.

Note: Remember that the last element of the dependencies in package.json is not terminated by a “,”.

From the command line “node app.js” will execute the application you have created. Much of the documentation has been provided on nodejs.org and numerous other sites.

NodeJS is relatively easy once you have understood JavaScript, and those who are migrating from other languages like C , C++ will have to put in extra effort to understand the flexibility of JavaScript and the Asynchronous concept which JavaScript provides. You may always turn to sites like w3schools and stackoverflow to learn about the various JavaScript functions, methods and tricks to over come problems while coding in NodeJS.

When I started working on NodeJS , there were numerous hurdles that I had to overcome, and this article aims to culminate the solutions which were used. There are a few other aspects of NodeJS viz, command-line tools/apps created in NodeJS, databases, webserver and file I/O haven’t been discussed in here.

Aparajit i has worked in IT Security for more than 10 years with varied experience. Finding newer methods for detection of malware is a passion. Spare time is reserved for tracking botnets, CnC servers and writing articles for Infosec. Contact via Twitter : @iaparajit

About InfoSec

InfoSec Institute is the best source for high quality information security training. We have been training Information Security and IT Professionals since 1998 with a diverse lineup of relevant training courses. In the past 16 years, over 50,000 individuals have trusted InfoSec Institute for their professional development needs!

Join our newsletter

File download

First Name

Last Name

Work Phone Number

Work Email Address

Job Title

How will you fund your training?

Why Take This Training?

What is your timeline for training?

InfoSec institute respects your privacy and will never use your personal information for anything other than to notify you of your requested course pricing. We will never sell your information to third parties. You will not be spammed.

Comments

What is Skillset?

Skillset

Practice tests & assessments.

Practice for certification success with the Skillset library of over 100,000 practice test questions. We analyze your responses and can determine when you are ready to sit for the test. Along your journey to exam readiness, we will:

1. Determine which required skills your knowledge is sufficient
2. Which required skills you need to work on
3. Recommend specific skills to practice on next
4. Track your progress towards a certification exam