NodeJS
NodeJS
md 2025-05-13
Node.js
Why Node.js?
JavaScript Everywhere (Full Stack JavaScript)
Unified Language: Use JavaScript for both client-side and server-side development.
Code Reusability: Share libraries, functions, and models between the client and server.
Non-blocking I/O: Single-threaded event loop for handling many simultaneous requests.
Event Loop: Uses callbacks and promises for asynchronous execution.
Real-Time Interaction: Ideal for real-time apps like chat and gaming.
V8 JavaScript Engine: Compiles JavaScript directly into machine code for fast execution.
Efficient Memory Management: Non-blocking model reduces memory usage.
Scalability
Single-Threaded but Scalable: Event loop handles many I/O operations without separate threads.
Horizontal Scaling: Supports clustering for scaling across multiple CPU cores.
Node Package Manager (NPM): Large ecosystem of open-source libraries and packages.
Active Community: Constant improvement through community contributions.
Cross-Platform Development
Microservices Architecture
Lightweight Services: Ideal for building microservices that are independent and scalable.
I/O Bound Applications: Efficient handling of concurrent I/O tasks like file operations and
database queries.
What is Node.js?
Definition
Node.js: A runtime environment for executing JavaScript code on the server side using Google’s
V8 engine.
1/5
NodeJS.md 2025-05-13
Core Features
Core Architecture
V8 JavaScript Engine: Compiles JavaScript directly to machine code for fast execution.
Event Loop: Manages asynchronous I/O tasks with callbacks and event handling.
Libuv: Library that handles asynchronous I/O operations.
Modules and Packages: Create reusable and shareable modules through NPM.
Microservices and Serverless Architecture: Perfect for modular applications and cloud-based
serverless functions.
2/5
NodeJS.md 2025-05-13
http module: Imports the built-in HTTP module for handling requests and responses.
http.createServer(): Creates a server that handles incoming requests.
req: The request object containing request details like URL, method, and headers.
res: The response object used to send data back to the client.
res.writeHead(): Sets the status code and headers for the response.
res.end(): Sends the response body and ends the request.
server.listen(): Binds the server to a port (e.g., 3000) and begins accepting requests.
Exporting Modules:
// greeting.js
Importing Modules:
3/5
NodeJS.md 2025-05-13
PM2:
Writing Files:
4/5
NodeJS.md 2025-05-13
try {
let result = 10 / 0;
} catch (err) {
console.error("Error:", err);
}
Connect
GitHub omteja04
LinkedIn omteja
5/5