So what’s the official definition of Node.js? Their documentation describes it as follows:
Let’s break this definition down and tackle it one bit at a time. We’ll analyze the following Node.js attributes: its runtime, V8 engine, event-driven model, non-blocking I/O nature, and package ecosystem.
Plain English: Node.js provides you with the all the basic tools you need to interact with your operating system.
Chrome’s V8 JS Engine
What’s great is that Node.js includes a whole section on addons in their documentation. If you’re familiar with C++, you can use it to write your own modules to extend functionality and load it into Node.js.
As an aside, Anthony Alicea does an amazing job of explaining this in great detail in his course, Learn and Understand NodeJS.
The event-driven model can be a bit tricky to understand, but it is a really important concept because it is in great part what makes Node.js so efficient. Node.js has something called emitter objects. You can think of the emitter object as an object that sends out a flare to say ‘Hey, listen up, this task here was completed.’ Node.js also has listener functions. The listener functions is the code that gets executed once the particular event emmitter they are listening for sends up the flare. It helps me to think of an event as the combination of an emitter object and listener function.
- This message is processed by the V8 engine and libuv sends off a request to the operating system - ‘Please find and grab this file for me. When you’re done, let me know.’
- The operating system responds to libuv’s request and an event is added to libuv’s queue indicating that the task was complete.
- Libuv’s event loop, which is constantly checking the queue, sees that there is something in the queue.
- Libuv processes it and alerts our V8 Engine that ‘Hey, I finished doing what you asked of me. Here’s that file you asked for.’
- The message ‘I am finished reading the file’ is logged to the console.
Obviously, this is an extremely simplified example, but that’s essentially what is happening over and over again behind the scenes. Libuv’s event loop is actually really interesting to read about. I know, nerding out a bit here, but if you’re interested, I recomment you checkout this article, The Node.js Event Loop, Timers, and proces.nextTick().
Non-blocking I/O, that sounds kind of technical and complicated…it’s not! I/O stands for Input/Output. I was trying to find a good way of describing it, but this article, Overview of Blocking vs Non-Blocking, has a really great, easy to understand definition:
“I/O” refers primarily to interaction with the system’s disk and network supported by libuv.
Remember in the example above we saw that libuv interacts with the operating system? Those interactions are what I/O refers to.
But this is great! It means that we are able to interact with our operating system in a really efficient manner. Imagine how terribly long you would have to wait if Google could only process one search request at a time.
Plain English: Node.js is able to deal with multiple tasks all at once making it extremely efficient and asynchronous.
Endless possibilities! If you’ve worked with Node.js you’ve probably become familiar with npm. Node.js comes with a lot of core modules out of the box, but there are many many more modules out there that you can install and share using npm and then include in your project using require statements.
Why not just include all modules at once? There are a lot of modules and often times you only need a few to run your program. Having all these modules installed that are not being used would be a waste of space and decrease your program’s efficiency.
Plain English: There is more to Node.js than what it comes with out of the box, and you can access these packages of code that extend functionality using npm.
So What’s It Look Like
So what does it take to build a server using Node.js? It’s surprisingly simple! You may have imagined hundreds of lines of code and some really complex stuff, but at its most basic, it’s just a few lines.
To wrap up let’s just put all the ‘Plain English’ snippets from above together to get a really basic description of Node.js:
- Node.js provides you with the all the basic tools you need to interact with your operating system.
- Node.js is able to deal with multiple tasks all at once making it extremely efficient and asynchronous.
- There is more to Node.js than just what it comes with out of the box and you can access these packages of code that extend functionality using npm.
If you’re interested at taking a deeper dive into Node.js, I suggest looking through the various links/resources sprinkled in the article:
- V8 Engine - documentation
- Learn and Understand NodeJS - Udemy course by Anthony Alicea
- Libuv - documentation
- Overview of Blocking vs Non-Blocking
- The Node.js Event Loop, Timers, and proces.nextTick()
**Special thanks to @Michael Olorunnisola for helping edit!