Core Node Concepts

Node quickly became an established, viable and efficient web development platform. JavaScript on server environments was somewhat of a novelty and full stack JavaScript had never really been seriously considered.

What makes Node such a popular choice for many development projects?

One of nodes most compelling features is how it implements the concept on non-blocking I/O. Node embraces non-blocking I/O to improve performance for a number of application types.

JavaScript’s traditional event-based implementation makes a relatively convenient and well-understood syntax that suits asynchronous programming.

Key strengths of Node

  • Efficiently distributes small pieces of information
  • Enables Full Stack JavaScript Development
  • Easily scales to multiple processors or servers
  • standard library, module system, and npm

In order to dig deeper into the Node.JS framework it is important to at least have a basic understanding of the rudimentary concepts of JavaScript;

If you are totally new or unfamiliar with JavaScript then I suggest you review the The Modern JavaScript Tutorial

It is also worth taking time to understand the Event Driven Architecture on how it relates to JavaScript and Node.JS in particular

As with many software programming frameworks, you don’t necessarily need to be an expert in the programming language, but knowing enough of the basics stands you in good stead to mastering the framework, and over time you will master the programming language

In all probability if you’re reading this blog, you have worked with JavaScript in the past. Maybe you’ve worked with JQuery or have fiddled with some JavaScript files in a web application that uses HTML, CSS and JavaScript to make the client more dynamic and interactive by manipulating the browser Document Object Model (DOM).

If you’re like me, you have probably found working with JavaScript quite a frustrating experience and counter to other programming languages. You may have also spent alot of time fighting the different browsers’ incompatibles, probably compounded by the fact that you never really took the time to study JavaScript as a language, apart from some of the most basic features.

It pains me to say this, but I have to admit that I never really accepted JavaScript as a real programming language until about 5 years ago. I always regarded it some really basic scripting language, and not really capable of anything like any of the real programming languages I was proficient with at the time like C++, Java, C# or Python.

JavaScript was always a necessary evil I used with HTML to do stuff client side. I really didn’t like it!

All that changed when I found node.js. After a few weeks of developing using node.js I realized that JavaScript is actually really cool and far more powerful than I had previously thought.

I have since taken the time to really get to grips with JavaScript, trying to understand it’s quirks and really leverages it’s good parts! I do strongly suggest you do take time to buy and read JavaScript : The Good parts, JavaScript Patterns and have a copy of JavaScript: The Definitive Guide: Activate Your Web Pages (Definitive Guides) on your desk as reference.

What is Server Side JavaScript

The original intention for JavaScript was to be a scripting language for web browsers, with logic and computation abilities for a document-reading applications. However, despite this it did morph into a fully-featured programming language that can be used in other contexts.

Node.js is a highly efficient and scalable non-blocking I/O platform that was build on top of Google Chrome V8 engine and its ECMAScript. This means that most front-end JavaScript objects, functions and methods are available in Node.js.

Node.js works on a v8 environment – a virtual machine or a JavaScript engine that runs the JavaScript code. In order to deploy node applications to a server your hosting provider will need a v8 environment.

Non-blocking I/O

Node.js is a server-side framework, one of it’s main works is to handle any number of requests. In traditional I/O bound servers systems, a request can only be issued when the response to the previous request has arrived. This is typically referred to as blocking I/O.

Node.JS is a non-blocking I/O, If a request takes long time, Node.js sends that request in an event loop and moves on to handle the next request in the call stack. As soon as the pending request is done processing, it raises an event and the response is rendered on the browser.

Asynchronous Callbacks

In commonly in event driven architecture systems, servers perform tasks that might take a while to complete, like call an API or access a database. Node can process more incoming requests, allowing you to handle thousands of concurrent connections with very little overhead, as opposed to needing a thread on the server for each connection.



Install Node

I recommend installing Node.js on your workstation using Node Version Manager (NVM), which enables you to easily install multiple different versions of Node on your workstations.

Running Node

There are two primary ways to use Node.js on your machines: using the Node Shell or by saving JavaScript to files and running those.

Node Shell

Node shell, is also called Node REPL — REPL stands for Read-Eval-Print-Loop . Used quickly test things in Node. If you don’t
remember exactly how a function should be used, you can just quickly use the REPL and type in something to see what happens.

To launch the Node shell, you simply type node in a terminal window you are using:

The > is all the response you get. You can immediately start typing in some code:

The first line of the output is the result of the code you just executed. You made use the Node global variable console and its log function to print out Hello World! The output of this statement is Hello World! , which is printed to terminal window.

The undefined output is always the resulting value of the preceding statement. Every statement, function call, or expression has a value associated with it, which is printed out in the Node shell for you.

If there is no evaluated expression value or the called function does not return any particular value, the special value undefined is returned instead.

To exit out of the REPL environment use Ctrl + D



Edit and Execute JavaScript Files

The other option for running Node.js code is to simply use your favorite text editor, like Notepad, Gedit or even tools like Sublime and Visual Studio Code to write JavaScript code into a file, and then compile and run that code via the command line using the node command.

To demonstrate this, create and save the following to a file called hello.js :

Now, you can execute this file from the terminal window by ensuring you change into the directory i.e. cd NodeSample or whatever you named your directory then simply using the node to execute the file node hello.js

And you should see this output:

We are not using the Node shell, in this instance so you don’t get any information on the return values of the code executed.

Summary

We’ve briefly reviewed some key core node concepts and have installed node on your machine. We’ve also verified that node is working on your machine. You can now quickly run through the create a simple node.js module tutorial and build a simple web server.

Gary Woodfine

Freelance Full Stack Developer at threenine.co.uk
Helps businesses by improving their technical proficiencies and eliminating waste from the software development pipelines.

A unique background as business owner, marketing, software development and business development ensures that he can offer the optimum business consultancy services across a wide spectrum of business challenges.

Latest posts by Gary Woodfine (see all)

π