3.3. NodeJS Basics: NodeJS Basics

Página 6

NodeJS is a JavaScript software development platform that allows developers to create high-performance, highly scalable web applications. It is based on Google Chrome's V8 JavaScript engine and uses a non-blocking I/O model, which makes it lightweight and efficient. This chapter will introduce some basic NodeJS concepts.

1. V8 engine

The V8 engine is an open-source JavaScript interpreter developed by Google for Chrome. It compiles JavaScript code into machine code before executing it, instead of using an interpreter. V8 can run standalone, or it can be incorporated into any C++ application. NodeJS uses V8 as its execution engine, and also exposes many of its functionalities through an API.

2. Event Loop

One of the main concepts in NodeJS is the event loop. It is responsible for scheduling non-blocking tasks to run in the future. Each time a NodeJS script starts, an event loop is created. This loop is an entity that waits for tasks, executes them, and then sleeps, waiting for more tasks. Tasks can be scheduled to run immediately, or to run at a point in the future.

3. Non-Blocking I/O

NodeJS uses a non-blocking I/O model, which means that I/O operations such as reading and writing files, network access, etc., do not block the execution of other JavaScript code. Instead, these operations are delegated to the operating system, which performs them in the background. When the I/O operation is complete, a callback is scheduled to run in the event loop.

4. Modules

NodeJS uses a module system to organize code. Each JavaScript file can be a module, and each module has its own scope. This means that variables defined in one module are not visible in other modules unless explicitly exported. Modules can export functions, objects or values, which can then be imported by other modules.

5. NPM

NPM is the package manager for NodeJS. It is used to install and manage package dependencies. Every NodeJS app has a package.json file that lists its dependencies. When you run npm install, NPM looks at this file and installs all the dependencies listed there. NPM also provides a package repository where developers can publish and share their code packages.

6. Callbacks

NodeJS makes heavy use of callbacks. A callback is a function that is passed as an argument to another function, and that is called when a certain condition is met. In NodeJS, callbacks are often used to handle non-blocking I/O operations. When an I/O operation completes, the corresponding callback is scheduled for execution in the event loop.

These are some of the basic concepts of NodeJS. Understanding these concepts is critical to effective NodeJS application development. In the next chapter, we'll explore how these concepts are applied in practice when creating APIs in NodeJS.

Now answer the exercise about the content:

What is the function of Event Loop in NodeJS?

You are right! Congratulations, now go to the next page

You missed! Try again.

Next page of the Free Ebook:

73.4. NodeJS Basics: Creating an HTTP Server

Earn your Certificate for this Course for Free! by downloading the Cursa app and reading the ebook there. Available on Google Play or App Store!

Get it on Google Play Get it on App Store

+ 6.5 million
students

Free and Valid
Certificate with QR Code

48 thousand free
exercises

4.8/5 rating in
app stores

Free courses in
video, audio and text