What is Node.js? JavaScript on the Server Explained
What are We Gonna Study?
Hey there folks! Hope you are doing great and enjoying your dev life.
Today we'll be talking about NodeJS, didn't you ever wondered why and how a language meant to be run on a browser runs on your code editor too?
For answering those questions you have to bear with me till the very end of this blog, with all your focus concentrated here.
Here is a list of topics we'll cover today:
What Node.js is
Why JavaScript was originally browser-only
How Node.js made JavaScript run on servers
V8 engine overview (high level only)
Event-driven architecture idea
Real-world use cases of Node.js
What NodeJS Is?
NodeJS is neither a framework or a library, it is a free, open source, cross - platform runtime environment for JavaScript.
It allows you to run JavaScript outside the browser ecosystem.
Ryan Dahl in 2009, created NodeJs.
What are the benifits of NodeJS?
Built on V8 Engine: NodeJS is built using the chrome V8 engine, that means they have the same level of high-performance.
Asynchronous & Non-Blocking: NodeJS uses an event-driven architecture, non-blocking I/O model. That's why it can do multiple task at a time without waiting for one to finish.
Single-Threaded Event Loop: Unlike traditional servers, who create a new thread for each request, NodeJS operates on a single main thread. It uses an "event loop" to manage thousands of concurrent connections efficiently with minimal overhead.
Universal Language: It allows developers to run JavaScript on both frontend and backend.
Why JavaScript was Browser Only?
Well initially, JavaScript was meant to be run on browser only, because the main motive of JavaScript was to make web pages interactive.
Also, there wasn't any infrastructure which can allow it to run out of the browser (Before NodeJS).
Also, in initial days, it wasn't fast too! but when google introduced the V8 engine of JavaScript, it hypothetically became capable of handling complex backend (Hypothetical because at that time, NodeJS didn't existed).
How NodeJS Made JavaScript Run on Servers?
Ryan embedded the V8 Engine into the open source C++ engine, along with LibUV and made NodeJS.
He ran it in his machine, instead of a browser, this allowed JavaScript to get compiled in machine code, which computer can understand.
Basically, it removed the need of language switching ilke PHP or Java or Python, etc.
V8 Engine (High Level Overview)
V8 engine is a javascript engine developed by google for chrome.
it is open-source, built using C++ and it's performance is high.
It translates JavaScript code directly into native machine code.
Key Aspects:
JIT Compilation: It offers JIT (just in time) compilation of code, It means it translates JavaScript to machine code at the runtime.
Garbage Collection: It manages memory automatically, cleans unused object for performance and stability.
Performance Optimization: It optimizes code execution, including "hidden classes" to speed up property access and inline caching.
Sandboxing: It provides a secure, isolated environment for executing JavaScript code.
V8 Engine be like:
Event-Driven Architecture Idea
Event-Driven Architecture (EDA) is a design pattern in which system components communicate by producing and reacting to events.
Examples: Change in state (meaningful) such as user clicking a button, successful payment,
Independent services (producers) announce that something happened and needing to know each other services (consumers) will act on that environment.
This is the high level idea of it.
Real World Use Cases of NodeJS
NodeJS is commenly used for:
Real-time applications (chat apps, live updates)
APIs and microservices
Streaming services (audio/video)
Single-page application backends
Collaboration tools
But not only that, NodeJS allows you to build a complete, high performance backend that is why you can use it literally everywhere!
Wrap Up!
Wasn't it amazing to know about NodeJS?
I personally enjoyed it a lot! Hope you enjoyed it too!
That was it for today's blog, I'll catch you up in the next blog, until then keep coding and enjoying your life 💝

