An introduction to Node.js
Tags: javascript, node, webdev
Published: 2022-04-09 16:43:07

### How do you run your JS code? 
With a browser console!
And do you know how it is run ?
Probably if you are using chrome you may be heard of V8 engine. 

### Yes there is an engine that runs your code, but there are different engines, for instance:
**SpiderMonkey** is used in Mozilla Firefox
**JavaScript Core** is used in Apple Safari
**Chakra** is used in Internet Explorer
**V8** in used in Edge, Chrome, Brave, ...


### Then Developers were going to find a way to run JS without browser? How we can do that?
Yes, you guessed right With Node.js 

Google introduced Chrome browser with v8 engine in 2008. 
one year later, Node.js was introduced, running JS code outside the browsers.

Now you can run your JS code on client and backend side, as a Full stack.

But Node.js is not a programming language, it's not a framework, but a Runtime. It's written by C++.  

**It's an official definition: **
Node.js is an open-source, cross-platform, non-blocking, asynchronous, event driven, back-end JavaScript runtime environment that runs on the V8 engine and executes JavaScript code outside a web browser.

### What is open-source:
Node.js is released under a license in which the copyright holder grants users the rights to use, study, change, and distribute the software and its source code to anyone and for any purpose.

### What is cross-platform :
Node.js allows us to build cross-platform desktop apps on platforms such as NW.js or Electron. This allows you to reuse some code from the web app to the desktop version for Windows, macOS, and Linux

### What is [asynchronous](https://dev.to/hardy613/asynchronous-vs-synchronous-programming-23ed#:~:text=In%20programming%2C%20synchronous%20operations%20block,calling%20a%20provided%20callback%20function.) and Non-Blocking ?
Synchronous means run code line by line.
In programming, synchronous operations block instructions until the task is completed, while asynchronous operations can execute without blocking other operations
For example `stringify` is blocking code.
```js
JSON.stringigy({ food:'Ghorme Sabzi' })
```

and `setTimeout` is non-blocking code.
```js
setTimeout(myFunction, 1000);
```


### What is runtime, let's demystify that:
JavaScript runtime provides an environment for us to run JavaScript code. That gives us a little bit more than the V8 engine. 
JavaScript runtime runs code and whenever it needs some I/O it will communicate with `libuv`. 

`libuv library` is a multi-platform C library that provides support for async I/O based on event loops. 

### Is a web browser a JavaScript Runtime? 
Yes, A browser contains a JavaScript engine. The engine implements a JavaScript runtime, which includes the call stack, heap and event loop. 

In browser, we have some features for instance `window` object that allows us to use some functionality regarding browser but if you run it on Node.js runtime you will get an error, instead on Node.js runtime There is another object named `global` that works but not in browser. So V8 Engine is customized and integrated with some other features in each runtime (browser, Node.js)


### The NODE.JS SYSTEM
![Node.js System](https://www.simform.com/wp-content/uploads/2020/04/node.js-architecture.png)

**Node.js includes** V8, APIs, Bindings and libuv

**Node.js APIs**
 - fs
 - http
 - path
 - crypto
 -...


In order to write and run code in Node.js you can download [Node.js](https://nodejs.org/en/) or run your code online on [replit](https://replit.com/)

after downloading and installing Node.js you can open a terminal and type `node`, you should see something like this: 

```zsh
hash@HMacBook~ % node
Welcome to Node.js v14.17.0.
Type ".help" for more information.
> "ha"+"sh"
'hash'
```
Now you can write your js to be run, this environment called `repl` that stands for `read`, `eval`, `print` and `loop`.

### Who uses Node.js
Companies that use Node.js include the following:

LinkedIn, Netflix, Uber, Trello, PayPal, NASA,eBay, Medium,
Walmart, Mozilla 

Please share your Ideas about Node.js in comments. 

Thanks for reading