What exactly is Node.js ?

Node.js is a JavaScript runtime environment. Sounds great, but what does that mean? How does that work?

The Node.js run-time environment includes everything you need to execute a program written in JavaScript.

If you know Java, here’s a little analogy.

Node.js came into existence when the original developers of JavaScript extended it from something you could only run in the browser to something you could run on your machine as a standalone application. Now you can do much more with JavaScript than just making websites interactive. JavaScript now has the capability to do things that other scripting languages like Python can do.

Both your browser JavaScript and Node.js run on the V8 JavaScript runtime engine. This engine takes your JavaScript code and converts it into a faster machine code. Machine code is low-level code which the computer can run without needing to first interpret it. Node.js uses an event-driven, non blocking I/O model that makes it efficient.

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 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. If this was a web server, we would have to start a new thread for every new user. But JavaScript is single-threaded. So this would make JavaScript not very well suited for multi-threaded tasks. 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 to 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.

The JavaScript event loop

  • Push main() onto the call stack.
  • Push console.log() this runs right way and gets popped.
  • Push setTimeout(2000) is a Node API. When we call it, we register the event call pair back.
  • After registering in API SetTimeOut(2000) gets popped from the call stack.
  • Now the second setTimeOut(0) get registered in the same way.
  • After waiting for 0 second setTimeOut(0) gets moved to the call back queue.
  • The las console.log() runs and the main() gets popped from the call stack.

npm

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

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.

Require is a function, and it accepts a parameter “path” and returns module.exports.

Node Modules

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.

Written by: Nabeel Leghari

--

--

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