Trending September 2023 # Learn The Properties &Amp; Functions Ofprocess # Suggested October 2023 # Top 15 Popular | Lanphuongmhbrtower.com

Trending September 2023 # Learn The Properties &Amp; Functions Ofprocess # Suggested October 2023 # Top 15 Popular

You are reading the article Learn The Properties &Amp; Functions Ofprocess updated in September 2023 on the website Lanphuongmhbrtower.com. We hope that the information we have shared is helpful to you. If you find the content interesting and meaningful, please share it with your friends and continue to follow and support us for the latest updates. Suggested October 2023 Learn The Properties &Amp; Functions Ofprocess

Introduction to chúng tôi Process

Node.js gives us the privilege to retrieve process information such as the architecture, platform, version, process id, release. It also allows aborting processes and much more.

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

The process is the global object and accessible from anywhere. It is an instance of EventEmitter class. The process object provides access to a bunch of process events like ‘beforeExit’, ‘exit’, ‘disconnect’, ‘uncaughtException’, ‘unhandledRejection’, etc and functions like process.cwd(), process.abort() . Few properties of process objects that are widely used are chúng tôi process.stdout, process.stdin, process.stderr, etc.

Process Events

Let us list down some of the process events:

1. beforeExit

Usually, node exits when the event loop is empty and there is no further scheduled work to be performed. The beforeExit process event is used to perform asynchronous calls just before the node exits.

2. exit

The exit event takes place when the node is about to exit. It cannot perform any asynchronous calls like beforeExit event.

3. disconnect

If the chúng tôi process is spawned with an IPC (Inter-Process Communication) Channel, the disconnect event is emitted when the IPC Channel is closed.

4. uncaughtException

The uncaughtException event occurs when an uncaught JavaScript exception pops out all the way back to the Event Loop. The default behavior of an uncaught JavaScript exception is that, node prints the stack trace to stderr. But adding a handler explicitly which handles uncaughtException, overrides the node’s default behavior.

5. unhandledRejection

The unhandledRejection event is emitted when a promise gets rejected and no error handler is provided to handle the error. Usually, the catch() method is used to handle the error or exceptions that occurred during promises.

Demonstration of few events

Code:

const process = require('process') console.log(`With code ${code} process beforeExit event`); }); console.log(`With code ${code} process exit event`); }); console.log('This message is displayed first');

Properties of Node.js Process

Let us have a look through a few process properties:

1. process.env

The chúng tôi contains an object that contains the value of the user environment.

2. process.pid

The chúng tôi property returns the PID of the chúng tôi process.

3. process.ppid

The chúng tôi property returns the PID of the current parent chúng tôi process.

4. process.release

The process.release property returns the information to the current release, including urls and headers.

5. process.title

The process.title property returns the title of the current process.

6. process.version

The process.version gives you the currently installed version of the node. If you would really like to check it out, go to the command prompt and do the following steps:

node

process.version

We used global.process, because as we know the process object is accessible from anywhere.

Output:

7. process.versions

The process.versions also give us the version value, but as we can see below using global.process.versions we are able to retrieve the versions of all other modules or dependencies present in the node stack.

Output:

8. process.platform

Using chúng tôi platform, we can fetch the name of the operating system platform on which our chúng tôi process is running. The possible values are linux, win32, darwin, etc.

9. process.channel

The process.channel property is a reference to the IPC Channel if the chúng tôi process was been spawned by the IPC Channel. The process.channel property returns “undefined” as its value if the IPC Channel doesn’t exists.

10. process.connected

The process.connected returns its value “true” if the chúng tôi process is spawned with an IPC Channel and retains the value as long as it is connected. It returns “false” value upon the process.disconnect(). Once it is disconnected, we are no longer able to send messages using the process.send() method.

11. process.argv

The chúng tôi returns an array that consists of all command-line invocation arguments. The following screenshot illustrates the same:

12. process.argv0

It consists of the original value of argv[0] (read-only copy) when the chúng tôi starts.

13. process.arch

To fetch the value of operating system CPU architecture we use chúng tôi property. Possible values are : x32, x64, arm64, ia32, etc.

14. process.stdin

To read input, stdin stands for ‘standard in’.

15. process.stdout

To write output, stdout stands for ‘standard output’.

16. process.stderr

Exceptions are written using process.stderr(standard error)

Functions of Node.js Process

Let us have a quick look through some chúng tôi process functions:

a) process.cwd()

To retrieve the information of the current working directory we can use the process.cwd() method as shown:

The process.abort() method gives us the privilege to exit from the current process immediately.

c)  process.setuid(id)

To set the user identity of the node process we use process.setuid(). The value of the id can either be a username(string) or numeric type.

d) process.getuid()

To fetch the numeric user identity of the node process we use process.getuid() method.

e) process.seteuid(id)

 To set the effective user identity of the node process we use process.seteuid().

The value of the id can either be a username(string) or numeric type.

f) process.geteuid()

To fetch the numerical effective user identity of the node process we use process.geteuid() method.

g) process.setgid(id)

To set the group identity of the node process we use process.setgid(id) method.

The value of the id can either be a group name (string) or numeric type.

h) process.getgid()

To retrieve the numeric group identity of the node process, process.getgid() method is used.

i) process.setegid(id)

To set the effective group identity of the node process we use process.setgid(id) method. The value of the id can either be a group name (string) or numeric type.

j) process.getegid()

To retrieve the numerical effective group identity of the node process, process.getgid() method is used.

Conclusion

Thus we overlooked through the global process object of the node (being the instance of EventEmitter), few of its events as beforeExit, exit, uncaughtException, etc.

We also went through few properties of the chúng tôi process like chúng tôi chúng tôi and some of the process functions.

Recommended Articles

This is a guide to Node.js Process. Here we discuss the properties of the chúng tôi process and some of the process functions. You may also have a look at the following articles to learn more –

You're reading Learn The Properties &Amp; Functions Ofprocess

Update the detailed information about Learn The Properties &Amp; Functions Ofprocess on the Lanphuongmhbrtower.com website. We hope the article's content will meet your needs, and we will regularly update the information to provide you with the fastest and most accurate information. Have a great day!