0

What is NodeJS?

 2 years ago
source link: https://dev.to/artemismars/what-is-nodejs-2k29
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

What is NodeJS?

I've been studying nodejs half a year but I was just implementing APIs without understanding the foundation of tools I use.
I decided to write this post to check the foundation stuff of NodeJS! :)


NodeJS is an runtime environment that is built on V8 engine ( which compiles javascript codes into native machine codes).

NodeJS has some special features like event-driven, non-blocking(asynchronous) programming.

  1. What is Event driven programming?

Event driven programming in NodeJS means that events occurred by opening file or network communication, like request and response ,are handled via callback functions. here's some example.

File System

import fs from 'fs';

fs.readFile('/example/test.txt', (err, data) => {
  if (err) throw err;
  console.log(data);
});

Enter fullscreen mode

Exit fullscreen mode

Network event

app.get('/', (req, res) => {
  res.send("hello world");
});

Enter fullscreen mode

Exit fullscreen mode

NOTE!
This is not supported by NodeJS!
Because NodeJS takes only V8 engine parts. and DOM API is provided by Web browser thus it's not possible to access DOM API on Nodejs.
so the following code is a wrong example as nodejs example!

divTag.addEventListener('click', clickHandler);

Enter fullscreen mode

Exit fullscreen mode

You can see that those events are handled through callback
functions. Events are processed by Event Loop. and the Event loop
processes the Phases and these Phases have Queues. Event loops
will choose phases with Round Robin scheduling method.

A round robin is an arrangement of choosing all elements in a group equally in some rational order, usually from the top to the bottom of a list and then starting again at the top of the list and so on.

  1. Non-Blocking
    NodeJS supports a single thread programming. This means every process in NodeJS lives in a thread. But since it's single thread if the first process is working long time then next processes should wait until the first process ends. But NodeJS is Non-Blocking. So, Even tho the first process didn't end yet, next processes can be executed. This will make your app still working even tho there's heavy data being processed.

  2. Cross-Platform runtime environment.
    Your Nodejs codes will work on different operating systems like OSX, MacOS, Windows!

If you have some advice, please share it via comments!
Thanks for reading my post!


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK