<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: manish-carpenter</title>
    <description>The latest articles on DEV Community by manish-carpenter (@manishcarpenter).</description>
    <link>https://dev.to/manishcarpenter</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1007945%2F3c499bb5-1722-492d-be0f-14c9525fe03b.png</url>
      <title>DEV Community: manish-carpenter</title>
      <link>https://dev.to/manishcarpenter</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/manishcarpenter"/>
    <language>en</language>
    <item>
      <title>Deploy R (data science model) App and NodeJs app on window IIS using PM2.</title>
      <dc:creator>manish-carpenter</dc:creator>
      <pubDate>Sat, 14 Jan 2023 09:24:08 +0000</pubDate>
      <link>https://dev.to/manishcarpenter/deploy-r-data-science-model-app-and-nodejs-app-on-window-iis-514k</link>
      <guid>https://dev.to/manishcarpenter/deploy-r-data-science-model-app-and-nodejs-app-on-window-iis-514k</guid>
      <description>&lt;p&gt;In this tutorial I will let you know one of the possible way to deploy the R (data science model) and node.js application on the same window server in production environment. &lt;/p&gt;

&lt;p&gt;Let’s first discuss the over all project scope in order to understands that what we want to achieve.&lt;/p&gt;

&lt;p&gt;Requirement: We have one node.js application (NodeApp). That NodeApp responsible to realize any required custom logic like dealing data I/O operation through the defined APIs. For instance, one of the NodeApp API-1 get the data from the third party API. One of the microservice process this data and store it in database. API-2 of NodeApp get the request from user to predict the value, like todays's or tommorow's maximum daytime temperature. The prediction task is done by the R data science model (RApp). So, we have running two application one is NodeApp, second one is RApp. We have to deploy these two applications on personal window server (IIS).&lt;/p&gt;

&lt;p&gt;There is a number of ways to achieve this task. But I found the following way more suitable if we consider the scalability and manageability perspective of project.&lt;/p&gt;

&lt;p&gt;Phase 1: Program installation&lt;br&gt;
I assume that you have installed node, npm, and R; and able to run NodeApp from code-studio and RApp from r-studio. &lt;/p&gt;

&lt;p&gt;Step 1: Installation of R: Download and install &lt;a href="https://cran.r-project.org/"&gt;R&lt;/a&gt;. It is necessary that you should be able to run the r script from command prompt. In order to verify that, you have to run the ‘Rscript’ command on terminal and you will get response as given in below picture.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--lkOlvpKq--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8fvogt23r9n0j1yyrh8u.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--lkOlvpKq--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8fvogt23r9n0j1yyrh8u.png" alt="Image description" width="800" height="501"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If you get an error like : “'Rscript' is not recognized as an internal or external command, operable program or batch file” then following &lt;a href="https://stackoverflow.com/questions/36697244/rscript-is-not-recognized-as-an-internal-or-external-command-operable-program"&gt;solution&lt;/a&gt; will fix this error.&lt;/p&gt;

&lt;p&gt;Step 2:  Installation of pm2&lt;br&gt;
Setting up pm2 on window (IIS) is lil bit tricky comparatively to Linux or Mac. Pm2, which is install using npm doesn’t work on window as it works on Linux or Mac. So you have to follow the following steps in order to setup and run pm2&lt;/p&gt;

&lt;p&gt;First download the “pm2-installer” from &lt;a href="https://github.com/jessety/pm2-installer/archive/main.zip"&gt;git&lt;/a&gt;. The official guide can be found &lt;a href="https://github.com/jessety/pm2-installer"&gt;here&lt;/a&gt;. &lt;/p&gt;

&lt;p&gt;Run these three commands one by one from downloaded folder (I am assuming that you already have installed and running node and npm). It is recommended that you run the command with administrative privileged for that run CMD as administrative by clicking the right button of mouse.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;npm run configure&lt;/em&gt;&lt;/strong&gt;&lt;br&gt;
&lt;strong&gt;&lt;em&gt;npm run configure-policy&lt;/em&gt;&lt;/strong&gt;&lt;br&gt;
&lt;strong&gt;&lt;em&gt;npm run setup&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;After running these three commands successfully, go to services and find pm2 -&amp;gt; right click &lt;br&gt;
-&amp;gt; properties&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--etbvkcWu--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6p6n85j0jhuj1zd817iy.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--etbvkcWu--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6p6n85j0jhuj1zd817iy.png" alt="Image description" width="800" height="409"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Then you have to select ‘Local System Account’ instead of ‘This account’. Then click on Apply and then Ok.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--4hnX6EBF--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/csjgr94w4y5ws44rqih2.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--4hnX6EBF--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/csjgr94w4y5ws44rqih2.png" alt="Image description" width="630" height="720"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now we are good to go to introduces process to pm2, which we want to manage.&lt;/p&gt;

&lt;p&gt;Phase 2: Prepare RApp for the given scenario.&lt;br&gt;
Step 1. Produce ‘.rds’ file: &lt;br&gt;
I am assuming that you have already developed your R application or simply R script and have produced file(s) into ‘.rds’ format. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--bNEb48gg--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/yd6r9kpkm6di9o1otusz.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--bNEb48gg--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/yd6r9kpkm6di9o1otusz.png" alt="Image description" width="800" height="242"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Or you can follow the official document from &lt;a href="https://rstudio-education.github.io/hopr/dataio.html"&gt;here&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Step 2: Transform R script into RestAPI: Definitely transforming simple R code into web service using &lt;a href="https://www.rplumber.io/"&gt;plumber&lt;/a&gt; have enormous benefits. It will help to keep the code separate from the other app logic (which is NodeApp in our case) and allow to follow the microservice architecture style. In that sense the application will have the scalability feature.&lt;/p&gt;

&lt;p&gt;It is very simple to create RestAPI for R script using plumber, as given in below picture.&lt;/p&gt;

&lt;p&gt;Or simple you can follow the official document (which is always recommended) from &lt;a href="https://www.rplumber.io/articles/quickstart.html"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Example of APIs creation using plumber is given below.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--hgtECF0U--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/f2oh2hc5w1or9ahvvw2g.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--hgtECF0U--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/f2oh2hc5w1or9ahvvw2g.png" alt="Image description" width="800" height="430"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If you have installed plumber, you can use the pr() function to instantiate a new Plumber router programmatically ( or generate plumber APIs).  Bring the API to life using the pr-run() command and pass the port number as an argument:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;library(plumber)&lt;/em&gt;&lt;/strong&gt;&lt;br&gt;
&lt;strong&gt;&lt;em&gt;# 'anything.R' is the location of the file shown above&lt;/em&gt;&lt;/strong&gt;&lt;br&gt;
&lt;strong&gt;&lt;em&gt;pr("anything.R") %&amp;gt;%&lt;/em&gt;&lt;/strong&gt;&lt;br&gt;
  &lt;strong&gt;&lt;em&gt;pr_run(port=8000)&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Copy whole command and paste into R-studio console and you will see your application live and accessible via APIs:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--QAPeE-u4--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ye20mmeumhavm6jszmfo.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--QAPeE-u4--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ye20mmeumhavm6jszmfo.png" alt="Image description" width="800" height="176"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You will get a message that API is running on some port like ‘8000’ and you can make request in following ways for the defined APIs:&lt;/p&gt;

&lt;p&gt;&lt;a href="http://localhost"&gt;http://localhost&lt;/a&gt; :8000/connection-status or &lt;a href="http://localhost"&gt;http://localhost&lt;/a&gt; :8000/predict&lt;/p&gt;

&lt;p&gt;Step 3: Before we process with step 3, I am assuming that your NodeApp is ready with you. Put the .rds file and plumber API script file into R_app  folder at root of you NodeApp folder as give below; however, it is not compulsory and you can keep R_app anywhere, wherever you want. But accordingly you have to provide the path that we have discussed in further steps. Major required file to run RApp using pm2 are highlighted. We will see why and how to create "r-mp2-start-script.R" file in subsequent steps.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--lrebPvoV--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ofgzzixmy9y9i8p67sd2.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--lrebPvoV--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ofgzzixmy9y9i8p67sd2.png" alt="Image description" width="486" height="698"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Step 4: Initially, pm2 designed to deal node project but we can wrap the r code (which is responsible for running plumber) in a way that it can understood by the pm2. So that pm2 can manage RApp as well in term of restarting whenever server get restarted.&lt;/p&gt;

&lt;p&gt;In order to introduce R script to mp2, we have to create one more file with the following script (you can give any name to this file) I preferred r-mp2-start-script.R:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--72_tPHOp--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/iic50xgq9bzmm2itntnk.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--72_tPHOp--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/iic50xgq9bzmm2itntnk.png" alt="Image description" width="800" height="176"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;(It is recommended to provide full path instead of relative path)&lt;/p&gt;

&lt;p&gt;Step 4: In order to insure that PM2 can manage your R app, you can manually test it by running the “Rscript run-RiskPlumberAPI.R” command in terminal from the file location.&lt;/p&gt;

&lt;p&gt;After running this command your application should up and running. You can test it by calling the above-mentioned API, or APIs which you have defined in you R script. In our case:&lt;/p&gt;

&lt;p&gt;&lt;a href="http://localhost"&gt;http://localhost&lt;/a&gt; :8000/connection-status or &lt;a href="http://localhost"&gt;http://localhost&lt;/a&gt; :8000/predict&lt;/p&gt;

&lt;p&gt;You can close the terminal it will shut down R application,&lt;/p&gt;

&lt;p&gt;Phase 3: In this phase we will start both the app via pm2 as forever.&lt;/p&gt;

&lt;p&gt;Step1: Start RApp&lt;br&gt;
Using the following command, you can run the RApp via pm2:&lt;br&gt;
&lt;strong&gt;&lt;em&gt;pm2 start --interpreter="Rscript" /your-file-location/r&lt;/em&gt;&lt;/strong&gt;&lt;strong&gt;&lt;em&gt;-mp2-start-script.R&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;(Run the command prompt as administrator and execute the command from directory where r-mp2-start-script.R is reside)&lt;/p&gt;

&lt;p&gt;Now your RApp should up and running, you can test it by calling the above given (in step 4) APIs.&lt;/p&gt;

&lt;p&gt;Step2: Start NodeApp&lt;br&gt;
To run the NodeApp, you can create a file which will start the NodeApp. This file is also known as pm2 ecosystem file. You can generate is by running “pm2 init simple” it will generate the ecosystem file, which is basically a java script file. In this post I have created an ecosystem file as a .json. For that you simple create a file with any name. I named it “pm2 -start-config.json”.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--BMhQLk3v--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zduwd0wnlwdpr4arzkmh.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--BMhQLk3v--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zduwd0wnlwdpr4arzkmh.png" alt="Image description" width="800" height="287"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;pm2-start-config.json will contain the configuration of our NodeApp that we want to manage.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--MEK3vYk9--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1wkjn57kox7rz9mod8h3.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--MEK3vYk9--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1wkjn57kox7rz9mod8h3.png" alt="Image description" width="800" height="287"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You have to modify the script path of pm2 and npm-cli if needed, so check path on your server. Additionally, you have to modify the args field in pm2-start-config.json file according to your npm run script which defined in the package.json file.&lt;/p&gt;

&lt;p&gt;Now we are set to launch NodeApp by running the following command:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;pm2 start pm2-start-config.json&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;(Run the command prompt as administrator and execute the command from directory where pm2-start-config.json is reside)&lt;/p&gt;

&lt;p&gt;You will see the following output&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--ahtQR9Mp--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wvb6aqusw4ovb42zalw8.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--ahtQR9Mp--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wvb6aqusw4ovb42zalw8.png" alt="Image description" width="768" height="160"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now we run the “pm2 save” command to run the applications in background forever.&lt;/p&gt;

&lt;p&gt;And we are done!&lt;/p&gt;

</description>
      <category>deploy</category>
      <category>node</category>
      <category>r</category>
      <category>window</category>
    </item>
  </channel>
</rss>
