Node.js – Extensions

43611 - Emergenceingame


In the following article, Emergenceingames.com will introduce you to Node.js – Extension applications. In addition, readers can refer to some other articles on Emergenceingames.com to learn about RESTful API in Node.js.

Please refer to the following article of Emergenceingames.com to learn about Node.js – Extension application.

node js is free to use

Node.js – Extensions

1. Node.js – Extensions

Node.js runs in single-thread mode, but it uses an event-driven programming model for concurrency, enabling the creation of child processes that promote parallelism across multiple threads. system based on multi-core CPU.

The child process has 3 streams, child.stdin, child.stdout and child.stderr, which can be shared with the parent process’s stdio stream.

Node provides the following child_process module to create child processes:

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

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

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

node js is free to use 2

2. Exec() method in Node.js

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

child_process.exec(command[, options]callback)

In there:

command (string) is the command to run, with space-separated parameters.

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

+ cwd (string): child process’s current working directory.

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

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

+ shell (string): Shell to execute commands, where ‘/bin/sh’ defaults on UNIX, ‘cmd.exe’ on Windows. The shell will understand the switch command -c on UNIX or /s/c on Windows. On Windows, command line parsing 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.

– Callbacks: This function takes three arguments, error, stdout, and stderr, which are called with output when the process terminates.

The exec() method returns a buffer with the maximum size and waits for the process to terminate, trying to return all 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 results:

node master.js

Confirm output, 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])

In there:

– Command (string) is the command to run.

– args (array): argument string list.

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

+ cwd (string): child process’s current working directory.

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

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

+ customFds (array): files are not acceptable for child use for stdio.

+ detached (Boolean): child will be at the top of 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 block of data. spawn() starts receiving a response 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 results:

node master.js

Confirm output and start the server:

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. Node.js . fork() method

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])

In there:

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

– args (array): Argument string list.

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

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

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

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

+ execArgv (array): list of strings of arguments assigned to the executable (default: process.execArgv).

+ silent (Boolean): if the value is true, the child’s stdin, stdout and stderr 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 built into 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 results:

node master.js

Confirm output and start the server:

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.Emergenceingames.com/node-js-ung-dung-mo-rong-43611n.aspx
The above article Emergenceingames.com has just introduced you to Node.js – Extension applications. If you have any questions or questions that need answering, please leave your comments in the comment section below the article. In addition, readers can refer to some other articles already on Emergenceingames.com to learn about Express Framework in Node.js.

3;>3;>3;>

Related keywords:

node js is free to use

Extensions in Node.js, Node.js,

Source link: Node.js – Extensions
– Emergenceingames.com

Leave a Reply

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