Skip to content

A small project where i have been practice about nodejs, learning concepts relationed with that enviroment

Notifications You must be signed in to change notification settings

RamonTec/nodejs-course

Repository files navigation

NodeJS Course.

This project was created to put in practice new concepts relationend with Nodejs.

First, NodeJS is an runtime enviroment and open source. It's not a language.

It's made with C, C++ and Javascript using V8 Engine. We can build projects with nodejs an execuete in differents devices, like phone, servers, tv etc...

An important think about Nodejs is a single-threaded to execute javascript code.

Understanding JavaScript’s Single-Threaded Nature.

In computing, a thread is the smallest unit of processing that an operating system can schedule.

Node.js employs a single-threaded model and uses an event-driven, non-blocking I/O system to handle multiple tasks efficiently.

JavaScript is also single-threaded — it can execute only one task at a time. But if JavaScript can only do one thing, how can it handle multiple operations like network requests, timers, or user events?

The answer lies in delegation. JavaScript delegates asynchronous tasks to the browser APIs (in the client environment) or Node.js APIs (in the server environment). These systems handle async operations outside the JavaScript engine and notify it when tasks are ready to be processed.

Core concepts

Now, we have some concepts that we need to know about this topic.

  • Call Stack
  • Event loop
  • Callback Queues
  • Synchronous (Sync) Tasks
  • Asynchronous (Async) Tasks
  • Microtasks
  • Macrotasks

When you execute a JavaScript program, the main script itself can be interpreted as a macrotask containing multiple operations inside it.

The Call Stack executes all synchronous tasks (for example, console.log()), one at a time and in order. These tasks have the highest priority because they don’t depend on any external operation or delay.

But what happens with tasks that take time — like setTimeout() or network requests?

That’s where the browser environment and the event loop come in.

The Event Loop constantly monitors the system, checking whether the call stack is empty and if any async callbacks are ready to be executed.

⏱ Example 1: Macrotasks (Timers)

console.log('First sync task');

setTimeout(() => { console.log('A: First async Macrotask'); }, 2);
setTimeout(() => { console.log('B: Second async Macrotask'); }, 3);
setTimeout(() => { console.log('C: Third async Macrotask'); }, 0);

Here, the expected result is:

  • First sync task
  • C: Third async Macrotask
  • A: First async Macrotask
  • B: Second async Macrotask

Why? Because the call stack are checking line by line and if found a task like setTimeOut, this kind of task are handle by the brower API (out of the Javascript execution) until this task to be ready the event loop will move this task to callback queue into the macrotasks queue category.

Now, what happend if the call stack execute a promise?

⚡ Example 2: Microtasks (Promises)

console.log('First sync task');

setTimeout(() => { console.log('A: First async Macrotask'); }, 0);

Promise.resolve().then(() => {
  console.log('First async Microtask - I have priority! ☕️');
});

Promise.resolve().then(() => {
  console.log('Second async Microtask - I have priority! ☕️');
});

console.log('Second sync task');

The request or promise will be handle by the browser APi, like happen with macrotasks, at this point we are out of the javascript flow. Until the task will be ready for its execution the event loop will move that type task to the callback queue as microtask. An important thing about this is the microtask have major priority than macrotask... So If the call stack is empty the event loop will check the callback queue, first will move the microtasks ready to be executed until the list if empty then, the event loop will take one task from the macrotask queue and will move it to the call stack for its execution. The expected result is:

  • First sync task
  • Second sync task
  • First async Microtask - I have priority! ☕️
  • Second async Microtask - I have priority! ☕️
  • A: First async Macrotask

🧩 Summary

JavaScript’s single-threaded nature doesn’t mean it can only perform one operation at a time — it means the JavaScript engine executes code one statement at a time.

However, by leveraging the browser or Node.js APIs, JavaScript can delegate asynchronous work, continue executing other tasks, and then reclaim control when those async operations finish.

Understanding how the call stack, event loop, microtasks, and macrotasks work together helps you write faster, non-blocking, and efficient JavaScript code.

Module system in NodeJS.

Modules are fundamental to build robust and maintenable applications, we can encapsulate logic into code blocks with specific functionalities. Allow to use create complex structures from simple and reusable componentes or code blocks.

Features

  • Facilities code maintenance
  • Avoids conflicts in large-scale applications
  • Reuse logic across our application

Modules type

CommonJS (CJS)

Is the original nodejs system, which uses:

  • Require to import modules
  • Module.exports to export modules
  • File extension .js
  • Execution type: Sysnchronous
  • Default system since the beginning of nodejs

ES Module (ESM)

Is the modern module system, compatible with ECMAScript 6+

  • Use import and export
  • File extension .mjs
  • Execution type: Asynchronous by design
  • Stable since nodejs 14, available as experimental since nodejs 12

Native modules

Native modules come into the built of Nodejs, that means that we can use them without adding manually.

  • Fs: Reading file system
  • Http: To make request and creating http services
  • Path: Accesing to file paths
  • Os: Interact with operating system

Third party are those libraries that we can add into our project using npm install, for example: npm i is-odd

In my opinion we should use one module system between commonjs and es-module, to generate consistency throught the project.

Also, for new projects i'll recommend to work with es-module instead of using commonjs system.

Features

  • Facilities code maintenance
  • Avoids conflicts in large-scale applications
  • Reuse logic across our application

Modules type

CommonJS (CJS)

Is the original nodejs system, which uses:

  • Require to import modules
  • Module.exports to export modules
  • File extension .js
  • Execution type: Sysnchronous
  • Default system since the beginning of nodejs

ES Module (ESM)

Is the modern module system, compatible with ECMAScript 6+

  • Use import and export
  • File extension .mjs
  • Execution type: Asynchronous by design
  • Stable since nodejs 14, available as experimental since nodejs 12

Native modules

Native modules come into the built of Nodejs, that means that we can use them without adding manually.

  • Fs: Reading file system
  • Http: To make request and creating http services
  • Path: Accesing to file paths
  • Os: Interact with operating system

Thir party are those libraries that we can add into our project using npm install, for example: npm i is-odd

In my opinion we should use one module system between commonjs and es-module, to generate consistency throught the project.

Also, for new projects i'll recommend to work with es-module instead of using commonjs system.

About

A small project where i have been practice about nodejs, learning concepts relationed with that enviroment

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published