Node.js is a Javascript Runtime built on chrome’s V8 Javascript engine. It uses an event-driven, Non-Blocking I/O model that makes it lightweight and efficient. It can be anything ranging from Reading/Writing local files to making an Http request to an API. I/o takes time and hence blocks other functions, such as printing.

1 STASHED

What exactly is Node.js?

medium.com

In the blocking method, user2's data request is not initiated until user1's data is printed to the screen. 

1 STASHED

On the other hand, using a non-blocking request, you can initiate a data request for user2 without waiting for the response to the request for user1. You can initiate both requests in parallel. 

1 STASHED

1 LIKE

Otherwise, here’s a quick step-by-step explanation of how the JavaScript Event Loop works. 

  • Push main() onto the call stack. 
  • Push console.log onto the call stack. This then runs right away and gets popped. 
  • Push Settimeout onto the stack. Settimeout is a Node API.
  • After registering it in the APIs, setTimeout gets popped from the call stack. 
  • Now the second setTimeout gets registered in the same way. We now have two Node APIs waiting to execute. 
  • After waiting for 0 seconds, setTimeout gets moved to the callback queue, and the same thing happens with setTimeout . 
  • Only one statement can execute at a time. This is taken care of by the event loop.

1 STASHED

These are libraries built by the awesome community which will solve most of your generic problems. npm (Node package manager) has packages you can use in your apps to make your development faster and efficient. 

1 STASHED

Require does three things: 

  • It loads modules that come bundled with Node.js like 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. 

1 STASHED

A Node module is a reusable block of code whose existence does not accidentally impact other code. 

1 STASHED

Something that has happened in our app that we can respond to. There are two types of events in Node. 

  • System Events: C++ core from a library called libuv.. 
  • Custom Events: JavaScript core. 

1 STASHED

We have to do this, don’t we? 

1 STASHED

Deepstash helps you become inspired, wiser and productive, through bite-sized ideas from the best articles, books and videos out there.

GET THE APP: