Following is some formal definition which is given on the official Node.js website:
- Node.js works on an event-driven, non-blocking I/O model that gives it lightweight and efficient.
- Node.js’ packages, npm, is the biggest platform of open source libraries on the globe.
I/O points to input/output. It can be anything ranging from reading/writing local files to creating an HTTP request to an API.
Blocks other functions because I/O takes time.
Suppose a situation where we request a backend DB for the details of two users, user1 and user2 and then publish them on the screen or console. The response to this request needs time, but both of the user detail requests can be taken out separately and at equal time.
What Can Node.js Do?
- Node.js can produce the dynamic page content
- Node.js can create, open, read, write, delete, and close server’s files
- Node.js can handle form inputs
- Node.js can also perform your DB tasks without using any server-side language
In the I/O blocking system, user2’s records request is not received until user1’s records are published to the screen.
That’s where the non-blocking part comes in to handle this situation perfectly.
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.
In the Non-blocking I/O you can initiate a data request for both users without waiting for the response of other users.
The server can handle multiple requests at the same time, so the server is fulfilled the requirement of multi-threading for non-blocking I/O.
Below has given the event loop in step by step:
- Push main() over the call stack.
- Push console.log() over the call stack. This will runs soon and gets popped.
- Push setTimeout(3000) over the stack. setTimeout(3000) is a Node.js API. If we call it, we register the event-callback set. The event will wait for 3000 milliseconds, after that the callback function will be called.
- Next to registering it in the APIs, setTimeout(3000) gets popped from the call stack.
- Now the other setTimeout(0) takes registered in the same style. Now we have two Node APIs waiting to perform.
- After idle for 0 seconds, setTimeout(0) gets pushed to the callback queue, and the same thing repeats for setTimeout(3000).
- The functions wait in the callback queue for the call stack to be blank because only one statement can run at a time. Here is taken care of threading by the event loop.
- After last console.log() statement run, the main() gets popped from the call stack.
NPM stands for Node Package Manager which means maintaining your packages or modules for your application, it’s extremely sorted and managed way for manage your package or module dependencies, at the same time you can also achieve this goal by your convenient way, but npm is made exactly for it.
www.npmjs.com entertains thousands of available packages or modules to download and use.
Require performs three tasks:
- It includes packages that get bundled with Node.js like the file system and HTTP from the Node.js API.
- It loads third-party libraries like Mongoose and Express that you download from npm.
- It lets you include your files and modularize the application.
Require is a function, and it allows a parameter path and returns module.exports.
A Node module is a reusable piece of code whose presence does not incidentally affect other code pieces.
You can also write your modules and apply them to any different application. Node.js has a collection of pre-built-in modules which you can apply without any additional installation.
It is an open-source runtime engine written in C++.
Something that has occurred in our application that we can react to. There are two kinds of events in Node.js.
- System Events: C++ core code which comes from a library called libuv.