Node.js - Extension application

Node.js – Extension application


In the following article, Taimienphi.vn will introduce you more about Node.js – Application extension. In addition, readers can refer to some other articles on Taimienphi.vn to learn information about what RESTful API is in Node.js.


Let’s refer to the content of the article below of Taimienphi.vn to learn about Node.js – Open application offline.

Node.js – Extension application

Table of Contents:
1. Node.js – Extension application
2. exec () method in Node.js
3. The spawn () method in Node.js
4. The fork () method in Node.js

1. Node.js – Extension application

Node.js runs in single-thread mode, but it uses an event-driven programming model to handle concurrency, enabling the creation of child processes that promote parallel processing on System based on multi-core CPU.

Child processes have three streams, child.stdin, child.stdout, and child.stderr, that can be shared with the parent’s stdio stream.

Node provides the following child_process module to create the child process:

– exec: The child_process.exec method runs a command in shell / console and returns the buffer output.

– spawn: The child_process.spawn method launches a new process with the given command.

– fork: The child_process.fork method is a special case of spawn () to create child processes.

js node is currently in use 2

2. exec () method in Node.js

The child_process.exec method runs a command in the shell and outputs the buffer. It uses the following signature:

child_process.exec (command[, options], callback)

Inside:

command (string) is the command to run, with parameters separated by spaces.

options (audience) may include one or more of the following options:

+ cwd (string): The current working directory of the child process.

+ env (object): key – value Environment pairs.

+ encoding (string) (default: ‘utf8’).

+ shell (string): Shell to execute commands, where ‘/ bin / sh’ defaults on UNIX, ‘cmd.exe’ on Windows. Shell will understand the conversion command -c on UNIX or / s / c on Windows. On Windows, the command line parser must be compatible with cmd.exe.

+ timeout (number) (default: 0).

+ maxBuffer (Number) (default: 200 * 1024).

+ killSignal (string) (default: ‘SIGTERM’).

+ uid (number): set the user identity of the process.

+ gid (number): Set the group identity of the process.

– Callback: This function will take three arguments: error, stdout, and stderr, these are called with the output when the process is finished.

The exec () method returns a buffer of maximum size and waits for the process to finish, trying to return all the buffer data at once.

For example

Create 2 js files named respectively support.js and master.js:

File: support.js:

console.log (“Child Process” + process.argv[2] + “executed.” );

File: master.js:

const fs = require (‘fs’);

const child_process = require (‘child_process’);

for (var i = 0; i

var workerProcess = child_process.exec (‘node support.js’ + i, function

(error, stdout, stderr) {

if (error) {

console.log (error.stack);

console.log (‘Error code:’ + error.code);

console.log (‘Signal received:’ + error.signal);

}

console.log (‘stdout:’ + stdout);

console.log (‘stderr:’ + stderr);

});

workerProcess.on (‘exit’, function (code) {

console.log (‘Child process exited with exit code’ + code);

});

}

Next run the master.js file to see the result:

node master.js

Confirm the output, the server has started.

Child process exited with exit code 0

stdout: Child Process 1 executed.

stderr:

Child process exited with exit code 0

stdout: Child Process 0 executed.

stderr:

Child process exited with exit code 0

stdout: Child Process 2 executed.

3. The spawn () method in Node.js

The child_process.spawn method launches a new process with the given command, which uses the following signature:

child_process.spawn (command[, args][, options])

Inside:

– Command (string) is the command to run.

– args (array): list of argument strings.

– options (object) may include one or more of the following options:

+ cwd (string): The current working directory of the child process.

+ env (object): key – value Environment pairs.

+ stdio (array) Child’s stdio configuration string.

+ customFds (array): The file is not accepted for child use for stdio.

+ detached (Boolean): child will head the process.

+ uid (number): set the user identity of the process.

+ gid (number): Set the group identity of the process.

The spawn () method returns streams (stdout & stderr) and is used when the process describes the data block. spawn () starts to receive feedback as soon as the process starts executing.

For example

Create 2 js files named respectively support.js and master.js:

File: support.js:

console.log (“Child Process” + process.argv[2] + “executed.” );

File: master.js:

const child_process = require (‘child_process’);

for (var i = 0; i

var workerProcess = child_process.spawn (‘node’, [‘support.js’, i]);

workerProcess.stdout.on (‘data’, function (data) {

console.log (‘stdout:’ + data);

});

workerProcess.stderr.on (‘data’, function (data) {

console.log (‘stderr:’ + data);

});

workerProcess.on (‘close’, function (code) {

console.log (‘child process exited with code’ + code);

});

}

Next run master.js to see the result:

node master.js

Confirm output and server boot:

stdout: Child Process 0 executed.

child process exited with code 0

stdout: Child Process 1 executed.

stdout: Child Process 2 executed.

child process exited with code 0

child process exited with code 0

4. The fork () method in Node.js

The child_process.fork method is a special case of spawn () to create Node processes, it uses the following signature:

child_process.fork (modulePath[, args][, options])

Inside:

– modulePath (string): A module to run in child.

– args (array): List of argument strings.

– options (object) may include one or more of the following options:

+ cwd (string): The current active directory of the child process.

+ env (object): key – value Environment pairs.

+ execPath (string): used to create child process.

+ execArgv (array): List of argument strings assigned to the executable file (default: process.execArgv).

+ silent (Boolean): If the value is true, stdin, stdout and stderr of the child will be sent to the parent process, otherwise it will be inherited from the parent process.

+ uid (number): set the user identity of the process.

+ gid (number): Set the group identity of the process.

The fork method returns an object with the communication channel integrated in all methods of the normal ChildProcess variant.

For example

Create 2 js files named respectively support.js and master.js:

File: support.js:

console.log (“Child Process” + process.argv[2] + “executed.” );

File: master.js:

const fs = require (‘fs’);

const child_process = require (‘child_process’);

for (var i = 0; i

var worker_process = child_process.fork (“support.js”, [i]);

worker_process.on (‘close’, function (code) {

console.log (‘child process exited with code’ + code);

});

}

Next run master.js to see the result:

node master.js

Confirm output and server boot:

Child Process 0 executed.

Child Process 1 executed.

Child Process 2 executed.

child process exited with code 0

child process exited with code 0

child process exited with code 0

https://thuthuat.taimienphi.vn/node-js-ung-dung-mo-rong-43611n.aspx
The article above Taimienphi.vn has just introduced you to Node.js – Application extension. If you have any questions or questions, please leave your comments in the comment section below the article. Also readers can refer to some other posts already on Taimienphi.vn to learn about Express Framework in Node.js.

3;> 3;> 3;>

.

Related Posts

Leave a Reply

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