Here’s a formal definition as given on the official NodeJS website:
NodeJS uses an event-driven, non-blocking I/O model that makes it lightweight and efficient.
NodeJS’ package ecosystem, npm, is the largest ecosystem of open source libraries in the world.
I/O refers to input/output. This 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.
Consider a scenario where we request a backend database for the details of user1 and user2 and then print them on the screen/console. Of course, the response to this request takes time. But both of the user data requests can carry out independently and at the same time.
In the blocking method, user2’s data request is not initiated until user1’s data is printed to the screen.
That’s where the non-blocking part comes in.
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 also initiate both requests in parallel.
This non-blocking I/O eliminates the need for multi-threading since the server can handle multiple requests at the same time.
main()onto the call stack.
console.log()onto the call stack. This then runs right away and gets popped.
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.
- After registering it in the APIs,
setTimeout(2000)gets popped from the call stack.
- Now the second
setTimeout(0)gets registered in the same way. We now have two Node APIs waiting to execute.
- After waiting for 0 seconds,
setTimeout(0)gets moved to the callback queue, and the same thing happens with
- 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.
- The last
console.log()runs, and the
main()gets popped from the call stack.
- 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.
These libraries built by the awesome community. Further, This 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.
Require does three things:
- NodeJS loads modules that come bundled with Node.js like the file system and HTTP from the NodeJS 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.
Require is a function. So it accepts a parameter “path” and returns
A Node module is a reusable block of code whose existence does not accidentally impact other code.
Hence, you can write your own modules and use them in various applications. NodeJS has a set of built-in modules that you can use without any further installation.
V8 is an open source runtime engine. And it written in C++.
Something that has happened in our app that we can respond to. There are two types of events in Node.js.
- System Events: C++ core from a library called libuv. (For example, finished reading a file).