

{"id":103588,"date":"2021-11-22T09:00:56","date_gmt":"2021-11-22T03:30:56","guid":{"rendered":"https:\/\/data-flair.training\/blogs\/?p=103588"},"modified":"2021-11-22T09:58:35","modified_gmt":"2021-11-22T04:28:35","slug":"scaling-nodejs-applications","status":"publish","type":"post","link":"https:\/\/data-flair.training\/blogs\/scaling-nodejs-applications\/","title":{"rendered":"Scaling Nodejs Applications"},"content":{"rendered":"<p>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.<\/p>\n<h3>Methods for Scaling Nodejs Applications<\/h3>\n<h4>1. exec() method:<\/h4>\n<p>This method executes a command and it buffers the output.<\/p>\n<h4>Syntax:<\/h4>\n<p>child_process.exec(command[, options], callback)<\/p>\n<h4>Parameter:<\/h4>\n<ul>\n<li>Command:it is the command that you want to run.<\/li>\n<li>Options:it contains the cwd, env, encoding type, uid:<\/li>\n<\/ul>\n<p>a. env (Object) Environment key-value pairs<\/p>\n<p>b. cwd (String) Current working directory of the child process<\/p>\n<p>c. uid (Number) Sets the user identity of the process.<\/p>\n<p>d. gid (Number) Sets the group identity of the process.<\/p>\n<ul>\n<li>Callback: this callback function takes error, stdout, and stderr as arguments.<\/li>\n<\/ul>\n<h4>Example for exec method<\/h4>\n<p>We will be creating two js files, one index file and another child.js file.<br \/>\nWe will run the index.js file; this file will contain instructions to run the child.js file.<\/p>\n<h4>Code for index.js file:<\/h4>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">const child_process = require('child_process');\r\n\r\nfor (let i = 0; i &lt; 2; i++) {\r\nlet process = child_process.exec('node child.js ' + i, function\r\n(error, stdout, stderr) {\r\nconsole.log('stdout: ' + stdout);\r\nconsole.log('stderr: ' + stderr);\r\n});\r\n\r\nprocess.on('exit', function (code) {\r\nconsole.log('Child process exited with code ' + code);\r\n});\r\n}<\/pre>\n<h4>Code for child.js file:<\/h4>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">console.log(\"Child Process \" + process.argv[2] + \" execution completed.\");<\/pre>\n<h4>Output:<\/h4>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/10\/nodejs-exec-method.webp\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-103944\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/10\/nodejs-exec-method.webp\" alt=\"nodejs exec method\" width=\"1366\" height=\"702\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/10\/nodejs-exec-method.webp 1366w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/10\/nodejs-exec-method-768x395.webp 768w\" sizes=\"auto, (max-width: 1366px) 100vw, 1366px\" \/><\/a><\/p>\n<h4>2. spawn() method:<\/h4>\n<p>This method will create a new process with the command that you specify.<\/p>\n<h4>Syntax:<\/h4>\n<p>child_process.spawn(command[, args][, options])<\/p>\n<h4>Parameter:<\/h4>\n<ul>\n<li>Command: it is the command that you want to run.<\/li>\n<li>Args: it is an array of arguments.<\/li>\n<li>Options: it contains the cwd, env, uid, gid, etc:<br \/>\na. env (Object) Environment key-value pairs<\/li>\n<\/ul>\n<p>b. cwd (String) Current working directory of the child process<\/p>\n<p>c. uid (Number) Sets the user identity of the process.<\/p>\n<p>d. gid (Number) Sets the group identity of the process.<\/p>\n<h4>Example for spawn() method:<\/h4>\n<p>We will be creating two js files, one index file and another child.js file.<br \/>\nWe will run the index.js file; this file will contain instructions to run the child.js file.<\/p>\n<h4>Code for index.js file:<\/h4>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">const child_process = require('child_process');\r\n\r\nfor (let i = 0; i &lt; 2; i++) {\r\nlet process = child_process.spawn('node', ['child.js', i]);\r\n\r\nprocess.stdout.on('data', function (data) {\r\nconsole.log('stdout: ' + data);\r\n});\r\n\r\nprocess.stderr.on('data', function (data) {\r\nconsole.log('stderr: ' + data);\r\n});\r\n\r\nprocess.on('close', function (code) {\r\nconsole.log('child process exited exit code is ' + code);\r\n});\r\n}<\/pre>\n<h4>Code for child.js file:<\/h4>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">console.log(\"Child Process \" + process.argv[2] + \" execution completed.\");<\/pre>\n<h4>Output:<\/h4>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/10\/nodejs-spawn-method.webp\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-103945\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/10\/nodejs-spawn-method.webp\" alt=\"nodejs spawn method\" width=\"1366\" height=\"702\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/10\/nodejs-spawn-method.webp 1366w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/10\/nodejs-spawn-method-768x395.webp 768w\" sizes=\"auto, (max-width: 1366px) 100vw, 1366px\" \/><\/a><\/p>\n<h4>3. fork() method:<\/h4>\n<p>This method is a special case of spawn for creating a new process for the module that you specify.<\/p>\n<h4>Syntax:<\/h4>\n<p>child_process.fork(modulepath[, args][, options])<\/p>\n<h4>Parameter:<\/h4>\n<ul>\n<li>Modulepath: the path of the module that will be executed.<\/li>\n<li>Args: it is an array of arguments.<\/li>\n<li>Options: it contains the cwd,env, uid, gid,etc.<br \/>\na. env (Object) Environment key-value pairs<\/li>\n<\/ul>\n<p>b. cwd (String) Current working directory of the child process<\/p>\n<p>c. uid (Number) Sets the user identity of the process.<\/p>\n<p>d. gid (Number) Sets the group identity of the process.<\/p>\n<h4>Example for fork method:<\/h4>\n<p>We will be creating two js files, one index file and another child.js file.<br \/>\nWe will run the index.js file; this file will contain instructions to run the child.js file.<\/p>\n<h4>Code for index.js file:<\/h4>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">const child_process = require('child_process');\r\n\r\nfor (let i = 0; i &lt; 2; i++) {\r\nlet process = child_process.fork(\"child.js\", [i]);\r\nprocess.on('close', function (code) {\r\nconsole.log('child process terminated with code ' + code);\r\n});\r\n}<\/pre>\n<p>Code for child.js file:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">console.log(\"Child Process \" + process.argv[2] + \" execution completed.\");<\/pre>\n<h4>Output:<\/h4>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/10\/nodejs-fork-method.webp\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-103946\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/10\/nodejs-fork-method.webp\" alt=\"nodejs fork method\" width=\"1366\" height=\"702\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/10\/nodejs-fork-method.webp 1366w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/10\/nodejs-fork-method-768x395.webp 768w\" sizes=\"auto, (max-width: 1366px) 100vw, 1366px\" \/><\/a><\/p>\n<h3>Conclusion:<\/h3>\n<p>In this article, we have gone through a detailed overview of scaling a nodejs application. We hope you were able to learn.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>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.&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":103948,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[25259],"tags":[25782],"class_list":["post-103588","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-node-js-tutorials","tag-scaling-nodejs-applications"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.8 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Scaling Nodejs Applications - DataFlair<\/title>\n<meta name=\"description\" content=\"Learn about scaling nodejs applications. See exex, fork and spawn methods with code and examples for scaling node js application.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/data-flair.training\/blogs\/scaling-nodejs-applications\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Scaling Nodejs Applications - DataFlair\" \/>\n<meta property=\"og:description\" content=\"Learn about scaling nodejs applications. See exex, fork and spawn methods with code and examples for scaling node js application.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/data-flair.training\/blogs\/scaling-nodejs-applications\/\" \/>\n<meta property=\"og:site_name\" content=\"DataFlair\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/DataFlairWS\/\" \/>\n<meta property=\"article:published_time\" content=\"2021-11-22T03:30:56+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2021-11-22T04:28:35+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/10\/scaling-nodejs-applications.webp\" \/>\n\t<meta property=\"og:image:width\" content=\"1200\" \/>\n\t<meta property=\"og:image:height\" content=\"628\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/webp\" \/>\n<meta name=\"author\" content=\"DataFlair Team\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@DataFlairWS\" \/>\n<meta name=\"twitter:site\" content=\"@DataFlairWS\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"DataFlair Team\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"4 minutes\" \/>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Scaling Nodejs Applications - DataFlair","description":"Learn about scaling nodejs applications. See exex, fork and spawn methods with code and examples for scaling node js application.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/data-flair.training\/blogs\/scaling-nodejs-applications\/","og_locale":"en_US","og_type":"article","og_title":"Scaling Nodejs Applications - DataFlair","og_description":"Learn about scaling nodejs applications. See exex, fork and spawn methods with code and examples for scaling node js application.","og_url":"https:\/\/data-flair.training\/blogs\/scaling-nodejs-applications\/","og_site_name":"DataFlair","article_publisher":"https:\/\/www.facebook.com\/DataFlairWS\/","article_published_time":"2021-11-22T03:30:56+00:00","article_modified_time":"2021-11-22T04:28:35+00:00","og_image":[{"width":1200,"height":628,"url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/10\/scaling-nodejs-applications.webp","type":"image\/webp"}],"author":"DataFlair Team","twitter_card":"summary_large_image","twitter_creator":"@DataFlairWS","twitter_site":"@DataFlairWS","twitter_misc":{"Written by":"DataFlair Team","Est. reading time":"4 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/data-flair.training\/blogs\/scaling-nodejs-applications\/#article","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/scaling-nodejs-applications\/"},"author":{"name":"DataFlair Team","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/person\/b49855299264df5e27e3ec6c2cd9fde9"},"headline":"Scaling Nodejs Applications","datePublished":"2021-11-22T03:30:56+00:00","dateModified":"2021-11-22T04:28:35+00:00","mainEntityOfPage":{"@id":"https:\/\/data-flair.training\/blogs\/scaling-nodejs-applications\/"},"wordCount":465,"commentCount":0,"publisher":{"@id":"https:\/\/data-flair.training\/blogs\/#organization"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/scaling-nodejs-applications\/#primaryimage"},"thumbnailUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/10\/scaling-nodejs-applications.webp","keywords":["Scaling Nodejs Applications"],"articleSection":["Node Js Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/data-flair.training\/blogs\/scaling-nodejs-applications\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/data-flair.training\/blogs\/scaling-nodejs-applications\/","url":"https:\/\/data-flair.training\/blogs\/scaling-nodejs-applications\/","name":"Scaling Nodejs Applications - DataFlair","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/#website"},"primaryImageOfPage":{"@id":"https:\/\/data-flair.training\/blogs\/scaling-nodejs-applications\/#primaryimage"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/scaling-nodejs-applications\/#primaryimage"},"thumbnailUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/10\/scaling-nodejs-applications.webp","datePublished":"2021-11-22T03:30:56+00:00","dateModified":"2021-11-22T04:28:35+00:00","description":"Learn about scaling nodejs applications. See exex, fork and spawn methods with code and examples for scaling node js application.","breadcrumb":{"@id":"https:\/\/data-flair.training\/blogs\/scaling-nodejs-applications\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/data-flair.training\/blogs\/scaling-nodejs-applications\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/data-flair.training\/blogs\/scaling-nodejs-applications\/#primaryimage","url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/10\/scaling-nodejs-applications.webp","contentUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2021\/10\/scaling-nodejs-applications.webp","width":1200,"height":628,"caption":"scaling nodejs applications"},{"@type":"BreadcrumbList","@id":"https:\/\/data-flair.training\/blogs\/scaling-nodejs-applications\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Blog Home","item":"https:\/\/data-flair.training\/blogs\/"},{"@type":"ListItem","position":2,"name":"Node Js Tutorials","item":"https:\/\/data-flair.training\/blogs\/category\/node-js-tutorials\/"},{"@type":"ListItem","position":3,"name":"Scaling Nodejs Applications"}]},{"@type":"WebSite","@id":"https:\/\/data-flair.training\/blogs\/#website","url":"https:\/\/data-flair.training\/blogs\/","name":"DataFlair","description":"Learn Today. Lead Tomorrow.","publisher":{"@id":"https:\/\/data-flair.training\/blogs\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/data-flair.training\/blogs\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/data-flair.training\/blogs\/#organization","name":"DataFlair","url":"https:\/\/data-flair.training\/blogs\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/logo\/image\/","url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2016\/07\/Data-Flair.png","contentUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2016\/07\/Data-Flair.png","width":106,"height":48,"caption":"DataFlair"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/DataFlairWS\/","https:\/\/x.com\/DataFlairWS","https:\/\/www.linkedin.com\/company\/dataflair-web-services-pvt-ltd\/","https:\/\/www.youtube.com\/user\/DataFlairWS"]},{"@type":"Person","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/person\/b49855299264df5e27e3ec6c2cd9fde9","name":"DataFlair Team","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/ef46b745ddad2fad690af626c6ef29b91809ad0a9f5ef398d07817d8cad042f5?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/ef46b745ddad2fad690af626c6ef29b91809ad0a9f5ef398d07817d8cad042f5?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/ef46b745ddad2fad690af626c6ef29b91809ad0a9f5ef398d07817d8cad042f5?s=96&d=mm&r=g","caption":"DataFlair Team"},"description":"DataFlair Team is a group of passionate educators and industry experts dedicated to providing high-quality online learning resources on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. With years of experience in the field, the team aims to simplify complex topics and help learners advance their careers. At DataFlair, we believe in empowering students and professionals with the knowledge and skills needed to thrive in today\u2019s fast-paced tech industry. Follow us for Free courses, expert insights, tutorials, and practical tips to boost your learning journey.","url":"https:\/\/data-flair.training\/blogs\/author\/datafbdad\/"}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/103588","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/comments?post=103588"}],"version-history":[{"count":3,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/103588\/revisions"}],"predecessor-version":[{"id":103947,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/103588\/revisions\/103947"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/media\/103948"}],"wp:attachment":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/media?parent=103588"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/categories?post=103588"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/tags?post=103588"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}