Node Js Course – 2 What exactly is Node Js?

Hi, before Node, Javascript could not be use as a more general purpose programing language it was limited to what the browser allowed to do, so for example I can use Javascript to add a click event to the span or to redirect the user to a different page but there was  no way to use the Javascript programming language outside of the browser to build things like web servers that can access to the file system and connect to databases, so all of this things other programming languages can do this without any problems.  

With Node Js the Javascript developers can now use Javascript on the server side, so they can use the same Javascript programming language to create web servers, command line interfaces, application backends and more.

So in a high level – Node Js is a way to run Javascript code on the server.

Why this is possible? 

Node.js is a Javascript runtime built on Chrome’s V8 Javascript  engine.

The Job of the any Javascript engine is to take in Javascript code and compile down to machine code that your machine can actually execute. 

The V8 is written in C++ programming language, that means anyone out there could write the C++ application, they could incorporate de V8 Javascript engine into their application and it could extend the functionality that Javascript provide, and that is what exactly what chrome and Node JS do. 

Both Chrome and Node Js are largely written in C++. 

Node Js is not a programming language because at the end of the day we are going to write Javascript code.

The runtime is something that provides custom functionality, so in the case of Chrome it provides V8 with various objects and functions that allow Javascript developers in browsers to do things like add buttons click events or manipulate the DOM and those of these features does not make sense for Node JS because Node JS does not provides that things instead, the Node runtime provides various tools that Node developers need, libraries for setting up web servers integrating with the file system, so you can read and write from disc, and at the end of the day both Chrome and Node JS are just creating their own modify version of Javascript but the core is the same the Javascript language but with custom functions and objects injected. 

Differences and similarities between the browser and Javascript inside Node Js

So if we open the console in a web page we can insert Javascript code there, like: 

And if we open the terminal and write the command ‘node’, then we will see a little place when we can run Node statements (only node commands, not batch commands)  and also know as REPL (Read Eval Print Loop).

So if I write the same Javascript code that I wrote in the console log, the result will be the same, like this:

So all the Javascript methods are still here because those are provided by de V8 engine itself. 

So, let see some differences, if we open the browser and we write “window” in the console, this will be referenced to the browser window, and we will have a lot of methods and all of this methods make sense in the context of Javascript in the browser because we actually have a window to work with. 

But, what happens if I tried to get the window in node?

So If I type ‘window’ in the terminal I will have an error, because window is something specific provided by the chrome runtime when Javascript is running in the chrome application and Node does not have window, not need window so is not provided.

In Node we have something similar called ‘global’ that provide us methods and properties, while this ‘global’ is not defined in the browser. 

The browser have the ‘document’ that allow us to work with the DOM while Node does not have a DOM. 

But Node have something similar to document called ‘process’ that will help us to manipulate the Node process. 

So, Node JS is Javascript on the server and this is possible because Node JS use the V8 Javascript engine to run all the Javascript code that you provide. 

Node JS is able to teach Javascript new things by providing C++ bindings to V8, this allow Javascript to do anything that C++ can do, like go through the file system, so Javascript can do this through Node. 

By Cristina Rojas.