26 Apr 2018
5 min read
If you know Java, here’s a little analogy.
## Why Node.js?
Here’s a formal definition as given on the official Node.js website:
Node.js uses an event-driven, non-blocking I/O model that makes it lightweight and efficient.
Node.js’ package ecosystem, npm, is the largest ecosystem of open source libraries in the world.
I/O refers to input/output. 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.
Consider a scenario where we request a backend database for the details of user1 and user2 and then print them on the screen/console. The response to this request takes time, but both of the user data requests can be carried out independently and at the same time.
Blocking I/O (left) vs Non-Blocking I/O (right)
## Blocking I/O
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.
## Non-blocking I/O
On the other hand using a non-blocking request, you can initiate a data request for user2 without waiting for the response the request for user1. You can 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.
If you have 26 minutes, watch this excellent video explanation of the Node Event Loop:
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.
Require does three things:
A Node module is a reusable block of code whose existence does not accidentally impact other code.
You can write your own modules and use it in various application. Node.js has a set of built-in modules which you can use without any further installation.
V8 is an open source runtime engine written in C++.
Something that has happened in our app that we can respond to. There are two types to events in Node.
We have to do this, don’t we?
Make a file app.js and add the following to it.
Open your node terminal, change the directory to the folder where the file is saved and run
Bam — you’ve just written Hello World in Node.js.
There are a ton of resources you can use learn more about Node.js, including freeCodeCamp.org.
Ground Floor, Verse Building, 18 Brunswick Place, London, N1 6DZ
108 E 16th Street, New York, NY 10003
Join over 111,000 others and get access to exclusive content, job opportunities and more!