Scaling Nodejs Applications

FREE Online Courses: Knowledge Awaits – Click for Free Access!

In this article, you will get to know how to scale a nodejs application. We have discussed the methods with code and examples for scaling node js application.

Methods for Scaling Nodejs Applications

1. exec() method:

This method executes a command and it buffers the output.

Syntax:

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

Parameter:

  • Command:it is the command that you want to run.
  • Options:it contains the cwd, env, encoding type, uid:

a. env (Object) Environment key-value pairs

b. cwd (String) Current working directory of the child process

c. uid (Number) Sets the user identity of the process.

d. gid (Number) Sets the group identity of the process.

  • Callback: this callback function takes error, stdout, and stderr as arguments.

Example for exec method

We will be creating two js files, one index file and another child.js file.
We will run the index.js file; this file will contain instructions to run the child.js file.

Code for index.js file:

const child_process = require('child_process');

for (let i = 0; i < 2; i++) {
let process = child_process.exec('node child.js ' + i, function
(error, stdout, stderr) {
console.log('stdout: ' + stdout);
console.log('stderr: ' + stderr);
});

process.on('exit', function (code) {
console.log('Child process exited with code ' + code);
});
}

Code for child.js file:

console.log("Child Process " + process.argv[2] + " execution completed.");

Output:

nodejs exec method

2. spawn() method:

This method will create a new process with the command that you specify.

Syntax:

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

Parameter:

  • Command: it is the command that you want to run.
  • Args: it is an array of arguments.
  • Options: it contains the cwd, env, uid, gid, etc:
    a. env (Object) Environment key-value pairs

b. cwd (String) Current working directory of the child process

c. uid (Number) Sets the user identity of the process.

d. gid (Number) Sets the group identity of the process.

Example for spawn() method:

We will be creating two js files, one index file and another child.js file.
We will run the index.js file; this file will contain instructions to run the child.js file.

Code for index.js file:

const child_process = require('child_process');

for (let i = 0; i < 2; i++) {
let process = child_process.spawn('node', ['child.js', i]);

process.stdout.on('data', function (data) {
console.log('stdout: ' + data);
});

process.stderr.on('data', function (data) {
console.log('stderr: ' + data);
});

process.on('close', function (code) {
console.log('child process exited exit code is ' + code);
});
}

Code for child.js file:

console.log("Child Process " + process.argv[2] + " execution completed.");

Output:

nodejs spawn method

3. fork() method:

This method is a special case of spawn for creating a new process for the module that you specify.

Syntax:

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

Parameter:

  • Modulepath: the path of the module that will be executed.
  • Args: it is an array of arguments.
  • Options: it contains the cwd,env, uid, gid,etc.
    a. env (Object) Environment key-value pairs

b. cwd (String) Current working directory of the child process

c. uid (Number) Sets the user identity of the process.

d. gid (Number) Sets the group identity of the process.

Example for fork method:

We will be creating two js files, one index file and another child.js file.
We will run the index.js file; this file will contain instructions to run the child.js file.

Code for index.js file:

const child_process = require('child_process');

for (let i = 0; i < 2; i++) {
let process = child_process.fork("child.js", [i]);
process.on('close', function (code) {
console.log('child process terminated with code ' + code);
});
}

Code for child.js file:

console.log("Child Process " + process.argv[2] + " execution completed.");

Output:

nodejs fork method

Conclusion:

In this article, we have gone through a detailed overview of scaling a nodejs application. We hope you were able to learn.

Your 15 seconds will encourage us to work even harder
Please share your happy experience on Google

follow dataflair on YouTube

Leave a Reply

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