Node.js Introduction


Wikipedia describes Node.js this way: “Node.js is an open-source, cross-platform JavaScript runtime environment for developing a diverse variety of tools and applications. Although Node.js is not a JavaScript framework, many of its basic modules are written in JavaScript, and developers can write new modules in JavaScript. The runtime environment interprets JavaScript using Google’s V8 JavaScript engine.”.

The first incarnations of JavaScript lived in browsers. JavaScript is a ”complete” language: you can use it in many contexts. Node.js really is just another context: it allows you to run JavaScript code in the backend, outside a browser.

The makers of node.js took JavaScript and allowed it to run on your computer, independent of the browser. Normally JavaScript runs in the browser and it can only access you web page. A whole new world opens up. Now you can for example access files on your computer which you normally cannot do with JavaScript running in the browser. You access a network and databases as well.

In order to execute the JavaScript you intend to run in the backend, it needs to be interpreted and, well, executed. This is what Node.js does. Node.js ships with a lot of useful modules, so you don’t have to write everything from scratch, like for example something that outputs a string on the console. Thus, Node.js is really two things: a runtime environment and a library.

Unlike C and other compiled languages, Javascript runs in a container – a program that reads your JavaScript code and runs it. This program must do two things:

  1. Engine – parse your code and convert it to runnable commands
  2. Runtime – provide some objects to JavaScript so that it can interact with the outside world.

The first part is called Engine and the second is Runtime. For example, the Chrome Browser and Node.js use the same Engine – V8, but their Runtimes are different: in Chrome you have the window, DOM objects etc, while node gives you require, Buffers and processes. Chrome and Node.js therefore share the same engine (Google’s V8), but they have different runtime (execution) environments. In a way, the runtime is to the engine what the linker is to the compiler in a traditional compiled language.

The runtime environment provides the built-in libraries that are available to the program at runtime (during execution). So, if you’re going to use the Window object or the DOM API in the browser, those would be included in the browser’s JS runtime environment. A Node.js runtime includes different libraries, say, the Cluster and FileSystem APIs. Both runtimes include the built-in data types and common facilities such as the Console object.

Wikipedia goes on to say: “Node.js has an event-driven architecture capable of asynchronous I/O. These design choices aim to optimize throughput and scalability in Web applications with many input/output operations, as well as for real-time Web applications (e.g., real-time communication programs and browser games).”.

JavaScript is normally confined to a browser. Node.js allows you to run JavaScript independent of the browser so that it can run on your computer. Anything you were doing with Ruby on Rails or PHP you can now do with JavaScript. To do this, they took the V8 JavaScript engine of Google and allowed it to be installed on your computer. Now you can do new things with JavaScript that you could not do before. For example you can access files on your computer, listen to network traffic, listen to HTTP requests, access databases directly and many other things.

Learning Node.js

Don’t spend a lot of time learning Node if you are just getting started with Node. Install it. Know what it is, but move on to learning things that you will need to know, such as Express and Angular 2.

YouTube beginner videos on Node.js

Node.js Fundamentals – 13 minutes.

What is Node.js Exactly? – a beginners introduction to Nodejs – 13 minutes.

Here is a website with an article for beginners of Node.js:

At the Console

Once node.js is installed you can run it at the command line. You can also start writing JavaScript code as shown below.

Hello World

To get started quickly in writing some code and seeing results, have a look at the post called Node.js Hello World.

If you want to install Node.js, have a look at the blog post Node.js Installation.

Node.js is part of the Linux Foundation. The Linux Foundation hosts many of the most important open source projects in the world, including Linux.

Embedding Chrome V8

With Chrome V8, you can embed the code into your C++ program. Cool. That means that you could write a C++ program that asks the user to write some JavaScript code and you could use V8 to interpret that code. So it’s like adding a huge amount of C++ code to your program. More than that, you can take the V8 engine and make changes to how it interprets JavaScript by adding new features, mainly those features that JavaScript cannot handle because it was not designed to handle them. These are called hooks. For example, C++ was designed to work with hardware and files and folders on a drive.

Leave a comment

Your email address will not be published. Required fields are marked *