An Absolute understanding of basic of Node.js: Guide

https://www.whitelioninfosystems.com/

Why Node.js?

JavaScript Blocking I/O

JavaScript Non-blocking I/O

  1. Push main() onto the call stack.
  2. Push console.log() onto the call stack. This then runs right away and gets popped.
  3. Push setTimeout(2000) onto the stack. setTimeout(2000) is a Node API. When we call it, we register the event-callback pair. The event will wait 2000 milliseconds, then callback is the function.
  4. After registering it in the APIs, setTimeout(2000) gets popped from the call stack.
  5. Now the second setTimeout(0) gets registered in the same way. We now have two Node APIs waiting to execute.
  6. After waiting for 0 seconds, setTimeout(0) gets moved to the callback queue, and the same thing happens with setTimeout(2000).
  7. In the callback queue, the functions wait for the call stack to be empty. Because only one statement can execute a time. This is taking care of by the event loop.
  8. The last console.log() runs, and the main() gets popped from the call stack.
  9. The event loop sees that the call stack is empty and the callback queue is not empty. So it moves the callbacks (in a first-in-first-out order) to the call stack for execution.

npm

Require

  • Node.js loads modules that come bundled with Node.js like the file system and HTTP from the Node.js API.
  • It loads third-party libraries like Express and Mongoose that you install from npm.
  • It lets you require your own files and modularize the project.

Node Modules

V8 turbo-charges JavaScript by leveraging C++

Events

  • System Events: C++ core from a library called libuv. (For example, finished reading a file).
  • Custom Events: JavaScript core.

--

--

--

Director of Finance and HR at WHITELION

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Understanding Vue Slots

Plex’s 2021 Journey with Next.js and Vercel

Creating Responsive Web App with React + Tailwind + SASS

Reusing Higher Order Components in React applications with Bit

Insights from deep-diving into TypeScript

Navigation in React: React-Routers

Best Open-Source JavaScript NPM Packages Available on Github

i5 Macbook Air Review After 6 Months of use as a Developer

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Niravdholiya

Niravdholiya

Director of Finance and HR at WHITELION

More from Medium

Questions on Nodejs (easy and with an intent to revise the basics) - to be split in 2 sessions

Uninstall, deep-clean and re-Install Node.js — Ubuntu best practice

Memory Management in JavaScript

How does NodeJS require modules?

how does nodejs require modules — directory structure