Js on server | Node js
Episode 02 - JS on Server
Hey everyone! Welcome back to the Node.js tutorial series. In this episode, we will talk about a very important concept - JS ON SERVER!
I am super excited because this is where you will understand how JavaScript escaped from the browser and started running on servers!
What we will cover:
- What is a Server?
- What is an IP Address?
- What is V8 Engine?
- Node.js = C++ Application with V8
- What is ECMAScript?
- Node.js Superpowers
- How JavaScript Becomes Machine Code
- What is Low-Level Code?
What is a Server?
Let's start with the basics - What exactly is a server?
A server is essentially a remote computer. You can think of it as a computer whose CPU works remotely!
In simple words:
- Servers can be accessed over a network
- They provide resources and services to other computer programs
- A server provides data, services, resources, or programs to other computers known as clients
How Client-Server Communication Works:
======================================
Client (Your Computer) Server (Remote Computer)
┌──────────────────┐ ┌──────────────────┐
│ │ Request │ │
│ Browser │ ─────────────→ │ Server │
│ │ │ │
│ │ Response │ │
│ │ ←───────────── │ │
└──────────────────┘ └──────────────────┘
Uses IP Address to find the server!
Behind the scenes, when a computer needs to communicate with a server, it sends a request to the server using its IP address.
What is an IP Address?
Good question! An IP address, or Internet Protocol address, is a unique number that identifies every device connected to the internet.
Examples of IP Addresses: ======================== 192.168.1.1 → Your home router (Local) 8.8.8.8 → Google's DNS server 172.217.14.206 → Google.com server Every device on the internet has a unique IP address!
Think of it like a phone number for computers. When you type "google.com" in your browser, it actually finds Google's IP address and sends request there!
The JavaScript Problem - Before Node.js
Here's the important history:
Initially, JavaScript could ONLY be executed within web browsers!
This limited JavaScript to client-side tasks only. You could manipulate DOM, handle clicks, validate forms - but NOTHING on the server!
Before Node.js: ============== Client-Side (Browser) → JavaScript ✅ Server-Side → JavaScript ❌ (Not Possible!) Developers had to learn: - JavaScript for Frontend - PHP/Python/Java for Backend Two different languages for one application!
But then everything changed...
With the introduction of Node.js, JavaScript can now also be executed on servers!
This allows developers to use the same language for both client-side AND server-side programming. Amazing, right?
What is V8?
Now let's understand the heart of Node.js - the V8 Engine!
Key points about V8:
- The V8 JavaScript engine is written in C++
- It was created by Google for the Chrome browser
- V8 is super fast at executing JavaScript
But here's the most important thing:
V8 can be embedded into ANY C++ program!
This is a crucial feature! This is what made Node.js possible!
How V8 Works:
============
JavaScript Code
↓
V8 Engine (C++)
↓
Machine Code
↓
Computer Executes!
The process works as follows:
- You write JavaScript code
- V8 (written in C++) takes your code
- V8 compiles it down to machine code
- Computer can now execute it!
Node.js = C++ Application with V8 Embedded
Here comes the MIND BLOWING part!
Node.js is a C++ application with V8 embedded into it!
Node.js Architecture: ==================== ┌─────────────────────────────────────┐ │ Node.js (C++ App) │ │ │ │ ┌─────────────────────────────┐ │ │ │ V8 Engine │ │ │ │ (Also C++ Code) │ │ │ └─────────────────────────────┘ │ │ │ │ + Extra Superpowers (APIs) │ │ │ └─────────────────────────────────────┘
Ryan Dahl took the V8 engine and wrapped it with additional C++ code to give JavaScript superpowers it never had before!
What is ECMAScript?
Before we go further, let's understand ECMAScript!
ECMAScript is a standard for scripting languages, including:
- JavaScript
- JScript
- ActionScript
It is best known as the standard that defines JavaScript!
Think of it like this:
ECMAScript = Rules and Standards JavaScript = Language that follows those rules It's like: Traffic Rules = ECMAScript How you drive = JavaScript
ECMAScript standards are followed by JavaScript engines like:
- V8 (Chrome, Node.js)
- SpiderMonkey (Firefox)
- Chakra (Old Edge)
- JavaScriptCore (Safari)
This ensures consistent behavior across different environments!
Node.js Superpowers - Beyond V8!
This is very important to understand:
V8 engine HAS to follow ECMAScript standards.
But here's the thing - ECMAScript standards don't include things like:
- File system access
- Database connections
- API calls on servers
- Network programming
So V8 alone CANNOT do these things!
But Node.js CAN!
V8 Engine Alone: =============== ✅ Execute JavaScript ✅ Follow ECMAScript standards ❌ File system access ❌ Database connections ❌ HTTP servers ❌ API calls Node.js (V8 + Superpowers): =========================== ✅ Execute JavaScript ✅ Follow ECMAScript standards ✅ File system access (fs module) ✅ Database connections ✅ HTTP servers (http module) ✅ API calls ✅ Operating system access (os module) ✅ And much more!
Node.js added these superpowers on top of V8!
This is known as the JavaScript Runtime!
So when we say Node.js is a "JavaScript Runtime", we mean:
JavaScript Runtime = V8 Engine + Extra APIs and Features
How JavaScript Becomes Machine Code
Ever wonder how your JavaScript code comes to life? 🤔
We write JS, and then the V8 engine translates it into machine and assembly code - also known as low-level code - so the machine can understand it!
The Transformation Journey:
==========================
Your JavaScript Code (High-Level)
┌─────────────────────────────┐
│ const sum = (a, b) => a + b │
│ console.log(sum(5, 3)) │
└─────────────────────────────┘
↓
V8 Engine
↓
Assembly Code (Low-Level)
┌─────────────────────────────┐
│ MOV EAX, 5 │
│ ADD EAX, 3 │
│ CALL print │
└─────────────────────────────┘
↓
Machine Code (Binary)
┌─────────────────────────────┐
│ 10110000 00000101 │
│ 00000011 00000011 │
│ 11001101 00100001 │
└─────────────────────────────┘
↓
CPU Executes!
It's amazing how our high-level scripts transform into the instructions that power our apps!
What is Low-Level Code?
Low-level code refers to programming languages or code that is closer to machine language and hardware.
Key characteristics:
- Provides little abstraction from computer's architecture
- Allows fine-grained control over system resources
- Harder for humans to read and write
- Faster execution
Types of Low-Level Code:
1. Machine Language:
- The most basic form of low-level code
- Consists of binary (0s and 1s) instructions
- Computer's CPU can directly execute this
Machine Language Example: ======================== 10110000 01100001 00000011 11000011 11001101 00100001 Just 0s and 1s! Only CPU understands this.
2. Assembly Language:
- A step above machine language
- Uses symbolic representations (mnemonics)
- Somewhat easier to understand than binary
- Still very close to hardware
Assembly Language Example: ========================= MOV AX, 5 ; Move 5 into register AX ADD AX, 3 ; Add 3 to AX INT 21h ; System interrupt Human-readable mnemonics instead of binary!
The Complete Picture
High-Level vs Low-Level:
========================
High-Level Languages (Easy for humans):
- JavaScript
- Python
- Java
- C++
↓ Compilation/Interpretation
Low-Level Languages (Easy for machines):
- Assembly Language
- Machine Code (Binary)
↓
Hardware (CPU) Executes!
V8's job is to take your high-level JavaScript and convert it to low-level machine code that CPU can run!
Quick Recap
| Concept | Explanation |
|---|---|
| Server | Remote computer that provides services to clients |
| IP Address | Unique number identifying devices on internet |
| V8 | JavaScript engine written in C++, made by Google |
| Node.js | C++ application with V8 embedded + extra APIs |
| ECMAScript | Standard that defines JavaScript rules |
| JS Runtime | V8 Engine + Additional features (fs, http, etc.) |
| Low-Level Code | Machine code and assembly - close to hardware |
Interview Questions
Q: What is a server?
"A server is a remote computer that provides data, services, resources, or programs to other computers called clients over a network. It can be accessed using its IP address."
Q: What is V8 engine?
"V8 is a JavaScript engine written in C++ by Google. It compiles JavaScript code into machine code. The important feature is that V8 can be embedded into any C++ program."
Q: What is Node.js?
"Node.js is a C++ application with V8 engine embedded into it. It adds superpowers like file system access, HTTP servers, and database connections that V8 alone cannot do due to ECMAScript limitations."
Q: What is ECMAScript?
"ECMAScript is a standard for scripting languages that defines the rules JavaScript must follow. All JavaScript engines like V8, SpiderMonkey must follow ECMAScript standards for consistent behavior."
Q: What is JavaScript Runtime?
"JavaScript Runtime is the V8 engine plus additional APIs and features. Node.js is a JavaScript runtime that provides capabilities beyond what ECMAScript defines."
Key Points to Remember
- Server = Remote computer providing services
- IP Address = Unique identifier for devices on internet
- V8 = C++ JavaScript engine by Google
- V8 can be embedded into any C++ program
- Node.js = C++ app with V8 embedded + superpowers
- ECMAScript = Standards that JavaScript follows
- V8 follows ECMAScript - cannot do file system, DB etc.
- Node.js adds superpowers - fs, http, os modules
- JS Runtime = V8 + Extra APIs
- V8 converts JS to machine code (low-level)
What's Next?
Now you understand how JavaScript runs on the server and what makes Node.js special! In the next episode, we will:
- Install Node.js on your machine
- Write our first Node.js program
- Understand modules and require()
Keep coding, keep learning! See you in the next one!
Post a Comment