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.
- 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);
});
Network event
app.get('/', (req, res) => {
res.send("hello world");
});
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);
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.
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.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!
Top comments (7)
But how can the example with the document object be used with NodeJS? The application does not have access to a DOM, which is different from JS executed in the browser.
Thanks for your feedback! I was totally wrong on this. It was because of my poor understanding of NodeJS thanks! I also studied about DOM and Nodejs more.
I will write this topic in the next post!
No worries, this gives us both the chance to learn!
thanks! I just fixed my post. if you have any other feedbacks glad to hear it always.
For an example of event driven programming see How to use fs.createReadStream? The capabilities revolve around
EventEmitter
.Both examples you provided simply use callbacks to deliver a result or error - the most basic form of asynchronous processing where you provide a callback function to continue processing later once either result or error become available, so e.g.
fs.readfile()
can simply return - non-blocking - without reading any of the file yet (The Node.js Event Loop, Timers, andprocess.nextTick()
).fs.readFileSync()
is the synchronous, blocking versionfsPromises.readFile()
is the promises based version that can be used withasync
/await
.Thanks for reading my post. I have some fix. Hope you didn't get any wrong idea from my post. (I am still learning Nodejs).