Node.js Tutorial
Estimated reading: 4 minutes 23 views

⚙️ Node.js Core Concepts & Features – Master the Foundations of Backend JavaScript

🧲 Introduction – Why Core Concepts Matter in Node.js

Before building REST APIs, real-time apps, or file servers with Node.js, you must first understand its core architecture and features. These core concepts—including modules, buffers, the event loop, and global objects—form the foundation of everything you build with Node.js.

🎯 In this guide, you’ll learn:

  • How Node.js modules work (built-in & custom)
  • What Buffers, Streams, and Global Objects do
  • How the Event Loop and EventEmitter power async behavior
  • Why utility modules simplify low-level tasks
  • The role of the Web Module in HTTP services

📘 Topics Covered

🔹 Topic📖 Description
🧩 Node.js – ModulesReusable JavaScript libraries and built-in APIs
🌍 Node.js – Global ObjectsSystem-wide variables like __dirname, setTimeout, etc.
📤 Node.js – Console & ProcessLogging and managing runtime behavior
🔌 Node.js – Buffers & StreamsHandle raw binary data and data flow
🔄 Node.js – Callbacks ConceptNon-blocking code execution via function callbacks
🧠 Node.js – Events & Event LoopPowering async actions with event-driven architecture
🧮 Node.js – Utility ModulesBuilt-in tools: OS, Path, DNS, etc.
🌐 Node.js – Web ModuleCreating HTTP servers and handling requests/responses

🧩 Node.js – Modules

Modules are reusable JavaScript components. You can load:

  1. Built-in modules (e.g., fs, http, path)
  2. User-defined modules (your own JS files)
  3. Third-party modules (via npm)
// Import built-in 'fs' module
const fs = require('fs');

// User-defined module
const myUtil = require('./myUtil.js');

✅ Every file is treated as a separate module in Node.js.


🌍 Node.js – Global Objects

These are accessible from anywhere in your app:

  • __filename: Absolute path of the current file
  • __dirname: Directory of the current module
  • setTimeout(), setInterval(): Time-based functions
  • global: The top-level global object
console.log(__filename);
setTimeout(() => console.log("Delayed message"), 1000);

📤 Node.js – Console & Process

The console object is used to log output.

console.log("Logging in Node.js");

The process object gives control over the current Node process.

console.log(process.platform); // e.g., 'win32'
process.exit(); // Ends execution

✅ Use process.argv for command-line arguments.


🔌 Node.js – Buffers & Streams

Buffers are used to handle binary data.

const buf = Buffer.from('Node.js');
console.log(buf.toString()); // Output: Node.js

Streams allow processing data piece by piece (good for large files).

const fs = require('fs');
const stream = fs.createReadStream('file.txt');
stream.on('data', chunk => console.log(chunk.toString()));

🔄 Node.js – Callbacks Concept

Callbacks are functions passed into other functions to execute after a task completes.

fs.readFile('demo.txt', 'utf8', function(err, data) {
  if (err) throw err;
  console.log(data);
});

✅ Callbacks enable non-blocking, asynchronous execution.


🧠 Node.js – Events, Event Loop & EventEmitter

The Event Loop lets Node.js perform non-blocking operations using events.

const events = require('events');
const emitter = new events.EventEmitter();

emitter.on('greet', () => console.log('Hello!'));
emitter.emit('greet');

✅ Node apps register listeners and emit events – ideal for chat apps, real-time updates, etc.


🧮 Node.js – Utility Modules

Node.js includes built-in modules like:

const os = require('os');
console.log(os.platform());

const path = require('path');
console.log(path.basename('/foo/bar/index.js')); // index.js

🔹 Others include: dns, net, url, querystring


🌐 Node.js – Web Module

You can create a simple HTTP server using the http module:

const http = require('http');
http.createServer((req, res) => {
  res.writeHead(200, {'Content-Type': 'text/plain'});
  res.end('Hello, Node.js!');
}).listen(3000);

🧪 Output:

Server running at http://127.0.0.1:3000/

📌 Summary – Recap & Next Steps

The core concepts of Node.js—from modules and events to buffers and global objects—are the building blocks of any application. Mastering them unlocks the power to write non-blocking, modular, scalable apps.

🔍 Key Takeaways:

  • require() is used to import any module (built-in or custom)
  • Global objects are always available without import
  • Buffers handle binary data, while streams process data in chunks
  • EventEmitter enables real-time behavior via events
  • The http module creates web servers without frameworks

⚙️ Real-World Uses:

  • Building file-processing CLI tools
  • Creating streaming servers or downloaders
  • Designing API gateways with custom events
  • Monitoring processes or OS metrics

❓ Frequently Asked Questions

What is a Node.js module?
✅ It’s a reusable JavaScript component that encapsulates logic and can be loaded via require().


What is the difference between Buffers and Streams?
✅ Buffers hold binary data in memory, while Streams process data bit-by-bit as it arrives.


How does the Event Loop work in Node.js?
✅ It waits for events and calls their respective listeners when triggered—making code non-blocking.


Why is process useful in Node.js?
✅ It provides control over the current Node.js runtime environment—useful for exiting, handling args, or managing signals.


How do I create a web server without Express?
✅ Use the built-in http module and http.createServer() method.


Share Now :

Leave a Reply

Your email address will not be published. Required fields are marked *

Share

⚙️ Node.js Core Concepts & Features

Or Copy Link

CONTENTS
Scroll to Top