The NodeJS execution process

What is the NodeJS execution process like and what can we do to control it through the global process object.

One of the first things that we are going to learn in NodeJS is to work and understand the execution process of the program, which has its special features in this language. In the introductory chapters we’ve already covered some pretty interesting concepts, but now we’ll see it applied to code.

Before we start, we want to re-emphasize the nature of NodeJS as a general-purpose programming language, with which we can make all kinds of applications. Typically we’ll start with programs that will run in your operating system’s shell, or terminal, but its applications are very extensive from there.

Think then that NodeJS is started through the command console and then the process that will be in charge of executing it is the one started with the console itself.

Single Thread

One of the characteristics of NodeJS is its “Single Thread” nature. When you launch a program written in NodeJS, you have a single thread of execution.

This, in contrast to other programming languages ​​such as Java, PHP or Ruby, where each request is served in a new process, has its advantages. One of them is that it allows you to meet greater demand with fewer resources, one of the points in favor of NodeJS.

In order for NodeJS Single Thread programming to produce satisfactory results, we must understand its non-blocking feature, which we covered when talking about asynchronous programming in the . In short, all operations that NodeJS can’t perform instantly free up the process to serve other requests. But as we have said in this article, we intend to land these ideas in some code that allows us to learn new things about NodeJS.

See also  Compile CSS with Webpack 5

Note: It should be noted that the single thread does not imply that Node cannot have several threads internally to solve its problems. That is to say, our main thread, for example when we are developing a server with Node, will be able to listen to requests, but once they are answered, Node will be able to internally launch other processes to carry out all kinds of actions that must be produced in response to those requests. requests. It will also be possible if we wish to have various programs executed in different processes, or clones of the same process to attend to various requests more quickly.

process object

The process object is a global variable available in NodeJS that provides us with various information and utilities about the process that is executing a Node. It contains various methods, events, and properties that help us not only to get data from the current process, but also to control it.

Note: Being a global object means that you can use it anywhere in your NodeJS code, without having to do the corresponding require().

In the Node documentation you can find everything about this process object:

Note: The NodeJS documentation is organized by platform versions. The above link directs you to the latest version of the Node API, but you should check the docs for the version you’re working with. However, the API state for the process object has been defined for a long time and will hardly change.

Query process data

You can do a lot with the process and query various useful data. For example, let’s look at the following code where we can examine various information about the current process and the platform where we are running this program:

See also  What does standalone mean in the world of software development?

console.log(‘process id: ‘, process.pid); console.log(‘process title: ‘, process.title); console.log(‘node version: ‘, process.version); console.log(‘operating system: ‘, process.platform);

Exit the execution of a Node program

Sometimes we may need to immediately exit the execution of a NodeJS program. We can achieve this by calling the exit() method of the process object.

process. exit(); Note: The normal behavior of a program will be to exit automatically when it has finished its work. Technically this means that the sequence of execution of instructions of a script has finished and that there is no pending work in the “event loop”. We talked about the event loop in the article on the .

It will basically cause the program to terminate, even if there are asynchronous operations that have not completed or it is listening to various events in the program.

The exit method can optionally receive an exit code. If we do not indicate anything, “0” is understood as the exit code.

process. exit(3);

exit event

Another of the basic things that you can do through the process object is to define events when various things happen, for example, the exit of the program.

Events in Javascript are defined in the standard way by using the on() method, indicating the type of event we want to listen for and a callback function that will be executed when that event is fired. Since the exit event belongs to the process, we will define it from it.

process.on(‘exit’, function(code) { console.log(‘exiting process with exit code’, code); })

In this case, the callback function associated with the exit event receives the error code that can be generated by invoking the exit() method that we met in the previous point.

See also  Break and continue

conclusion

We have learned some general knowledge about NodeJS and something in particular about the execution process of a program written in Node, whose functionality is available through the global process object.

We’ve seen several examples of properties dependent on process, the exit() method, very useful for exiting a program, as well as the definition of one of the several events available within process, which will allow us to do things when the NodeJS program stops. stop. With this we have a very general vision about the execution process of node, but you will see that there is much more in the official documentation.

Just for possible doubts, I leave here a complete code that you can execute to see the examples reported in this article:

“use strict” process.on(‘exit’, function(code) { console.log(‘exiting process with exit code’, code); }) console.log(‘process id: ‘, process.pid ); console.log(‘process title: ‘, process.title); console.log(‘node version: ‘, process.version); console.log(‘operating system: ‘, process.platform); process. exit(3); console.log(‘this will never run’);

Loading Facebook Comments ...
Loading Disqus Comments ...