Why every web developer should know Node.js

Node.js has gained a huge popularity among web developers over past few years. It is a runtime that enables server-side execution of JavaScript, which was traditionally confined only to web browsers. In fact, the core of Node.js functionality is done by a V8 engine that is used by Google Chrome to parse and run JavaScript on web pages.

As a server-side technology, Node.js has one obvious limitations that experienced full-stack or back-end web developers will notice. Unlike Java or ASP.NET, the language is interpreted rather than compiled.

Compiled code already consists of processor instructions that anyone, even the most experienced software developers, would struggle to read. This is, however, the format that the CPU understands with the minimal amount of pre-processing.

Interpreted code, on the other hand, is the code that is deployed in exactly the same state as it has been originally written in. Every statement is read and translated into a set of processor instructions in real time, which makes the execution somewhat slower.

Another disadvantage of interpreted languages is that the production code is stored in easily accessible text files, while compiled code is stored in inaccessible binary format. Interpreted language is then, obviously, not the best choice for any closed-source app. Therefore, if you are working with a back-end technology that compiles into something that is fast and obfuscated, you may ask, why should I bother learning Node.js?

Although Node.js is primarily a server-side technology, it is much more than that. The runtime has completely redefined how any kind of JavaScript application is developed, regardless of whether it is intended to be executed in the back-end or the front-end.

The runtime architecture is so flexible that development of any kind of JavaScript-based application became effortless. So, if you are skeptical about Node.js and never intend to use it as a server-side technology, read on. There are some Node.js aspects that you may not be aware about that will make your life as a web developer so much easier.

Flexibility achieved via Node Package Manager

What makes Node.js super-flexible and suitable for many types of applications is a tool that comes with it: NPM, which stand for Node Package Manager. For those ASP.NET developers who are familiar with NuGet packages or Java developers who are familiar with Maven repository will grasp NPM in no time.

This tool allows developers to publish libraries, frameworks and various other tools and add-ons that work with Node.js into an online repository. You can use NPM in your command line to download any of the countless packages that are publicly available.

One of the key files in a Node.js application is package.json. This is a configurational file where you specify all dependencies from NPM repository that you will need in order to be able to run your application.

When you launch your Node.js application, magic happens. The runtime will automatically download any dependency that is specified in the file. As well as this, an IDE’s compatible with Node.js will highlight errors in your code when you forgot to add a critical dependency to your source.

The way packages are managed via NPM is very useful in server-side deployment scenario. You cannot always know ahead of time what Node.js dependencies are already available on the server. So you only deploy the code of your application itself and any third party dependencies will resolve themselves if needed.

However, as more and more traditional client-side JavaScript add-ons, such as jQuery, were made available to be used in the server-side code, it became apparent that NPM technology, if used strictly in development environment, can be used to significantly simplify the process of writing client-side applications as well.

Completely modular JavaScript code

Traditionally, JavaScript code was very difficult to structure nicely, unlike, for example, C# code. Usually, there was a number of fairly large JavaScript files referenced by a HTML web page. Each file would contain parts of the logic that vaguely relate to each other, but a proper modular design was almost impossible to implement.

Defining objects with strict data structure and keeping each of them separate from the actual code was a no-go. This resulted in developers spending some time navigating through the code to see how the logic flows.

With Node.js, this limitation of JavaScript was confined to the past. While in development, your code can be split into several files, each containing either a data object or a specific piece of logic. The code is structured in the same way it is in C# or Java application and you can now fully implement single responsibility principle in JavaScript.

To improve the process even further, the concept of transpilation has been fully taken advantage of by various Node.js tools. The term is a combination of two words: translation and compilation.

Transpilation is when the code is written in a strongly-typed language that, just like any .NET server-side language, will throw compiler errors, which helps you to ensure that your syntax is sound. When ready, the code is built into the actual JavaScript that a browser would be able to understand.

The process works in a similar way to traditional compilation process, but, as the code is not actually converted into a language that is meant to run close to the hardware, translation, rather than compilation, is what actually happens.

TypeScript is an example of a strongly-typed language that is often used in Node.js development environment. It’s syntax only slightly differs from JavaScript. In fact, as this is a language embraced by Microsoft, the syntax looks like a hybrid between C# and JavaScript. TypeScript allows you to write your code in pure JavaScript, but why would you do that?

Once your client-side code is in production, there is no longer need to make it easily readable. Therefore, NPM contains various tools to bundle and minify your code.

Bundling is the process that takes various small JavaScript files and combines them into one large file. This allows you to write your HTML UI in exactly the same way you’ve always done it: by only referencing a limited number of JS files from your HTML.

The commonly accepted standard used by various client-side Node.js frameworks is to combine the code that has been downloaded from various NPM dependencies into a JS file called vendor bundle and to combine the actual custom application code into either app bundle or main bundle, depending on the framework.

So, the code files that you actually write are not the files that will be deployed. The output files will be deposited into a separate folder by a dedicated tool installed by NPM, not dissimilar to how C# code is compiled into DLL files, which are deposited into “bin” folder.

Minification is the process of changing your code in such a way that it remains the same functionally, but becomes much more compact visually. This is achieved by removing non-functional characters, such as white spaces and changing descriptive variable names into single-letter identifiers. This makes your code much smaller than the combined size of all of the files that were combined to produce it.

Also, it provides some degree of protection against unwanted attention. The process does not obfuscate the code to the same degree as compilation does, as the output code is still contained within text files. However, it makes the code difficult enough to read that most of people will not even bother. UglifyJS is one popular NPM library that is used to perform minification.

How Aurelia and Angular take advantage of Node.js infrastructure

If you are still doubtful about the utility of Node.js as a development environment for a client-side JavaScript application, be aware that this is now one of the primary ways of how Angular and Aurelia, some of the most popular JavaScript frameworks, are used.

Node.js environment for Angular is also what Microsoft has officially embraced. In MSDN Magazine, one of the official magazines for developers who use Microsoft technologies, Ted Newman has a column with a title “The Working Programmer – How To Be MEAN“, where he talks about the best ways to develop a web application using the latest version of Angular, which has embraced a modular design inspired by Node.js. In fact, MEAN stands for Mongo, Angular and Node.js.

Aurelia is a framework not too dissimilar from Angular. In fact, it was created by one of the people who used to work on version 2 of Angular, where modular design was first introduced. Arguably, Aurelia is the easiest one of the two to master, while having all the same functionality.

Both of the frameworks have their own set of dedicated and well-documented NPM tools that enable them to be used in a client-side application. Both frameworks are compatible with a whole range of general-use NPM packages, such as strongly-types JavaScript-derived languages, transpilers, bundlers and minifiers.

If you will pick one of the frameworks and will go through its official tutorial written specifically for Node.js environment, you will see for yourself how easy it is to get started. So, give it a go. I am pretty sure that you will be pleasantly.