<?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: Chuiko Liliia</title>
    <description>The latest articles on DEV Community by Chuiko Liliia (@liliyadev).</description>
    <link>https://dev.to/liliyadev</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.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F782588%2F44c43136-ac0c-44c5-be74-c836ea884e01.jpeg</url>
      <title>DEV Community: Chuiko Liliia</title>
      <link>https://dev.to/liliyadev</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/liliyadev"/>
    <language>en</language>
    <item>
      <title>How to Run a Python script from Node.js</title>
      <dc:creator>Chuiko Liliia</dc:creator>
      <pubDate>Tue, 14 Mar 2023 11:14:00 +0000</pubDate>
      <link>https://dev.to/halolab/how-to-run-a-python-script-from-nodejs-3f0a</link>
      <guid>https://dev.to/halolab/how-to-run-a-python-script-from-nodejs-3f0a</guid>
      <description>&lt;p&gt;Quite often we hear disputes about what is better to use Node.js or Python. &lt;br&gt;
But what if one day, you come up with a great idea to combine those, for instance to use some Python libraries in your  application, but you just do not have any idea how to integrate it with your Node.js application. Of course you can always build API on top of Python backend(Flack, etc), but in that case you need to build, host and manage one more application, when you just need to run a single Python script. That's why I want to give you step by step instructions on how to achieve this.&lt;/p&gt;

&lt;p&gt;To begin with, let's briefly define what are both languages.&lt;/p&gt;

&lt;p&gt;Node.js is a server-side platform developed on Google Chrome’s Javascript Engine. Node Js allows to develop data-intensive real-time web applications. It is written on JavaScript and allows to run applications on various operating systems such as Windows, Linux, and Mac OS.&lt;/p&gt;

&lt;p&gt;Python is an object-oriented programming language used to create dynamic web applications. The syntax of Python and dynamic typing make it an ideal programming language for scripting.&lt;/p&gt;
&lt;h2&gt;
  
  
  Why you might need to run Python scripts from Node.js
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Node js can't run heavy multitask as it works on a single thread basis, making it difficult for Node Js to execute CPU-bound tasks. Whenever any request is received, the system will complete the request once and only accept the other request. This process slows down the system and ends up in a massive delay.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Python is much more suitable for that back-end applications, that use numerical computations, big data solutions, and machine learning.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Python has one of the largest ecosystems out of any programming community and a lot of open-source libraries. For instance some of built-in AI libraries (Pylearn2, Tensorflow for neural networks, and Scikit-learn for data analysis)&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;
  
  
  Сall your Python script from Node.js using Child Process module
&lt;/h2&gt;

&lt;p&gt;You will find several libraries that can help you with this issue. As for me, I prefer to use built in &lt;a href="https://nodejs.org/api/child_process.html"&gt;child_process&lt;/a&gt;. The node:child_process module provides the ability to spawn subprocesses. This capability is primarily provided by the child_process.spawn() function.&lt;/p&gt;

&lt;p&gt;First, we need to create a server.js file as the server for our app. Where we suppose that &lt;code&gt;X&lt;/code&gt; and &lt;code&gt;y&lt;/code&gt; are large arrays.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;express&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;express&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;spawnSync&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;child_process&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;readFile&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;fs/promises&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;appendFile&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;fs/promises&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;join&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;path&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;app&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;express&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

&lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="kd"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;/&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;next&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;=&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
   &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;X&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="c1"&gt;// large array&lt;/span&gt;
   &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;y&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;]]&lt;/span&gt; &lt;span class="c1"&gt;// large array&lt;/span&gt;

   &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;appendFile&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
            &lt;span class="nx"&gt;join&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`scripts/args.json`&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
            &lt;span class="nx"&gt;JSON&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;stringify&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="nx"&gt;X&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;y&lt;/span&gt; &lt;span class="p"&gt;}),&lt;/span&gt;
            &lt;span class="p"&gt;{&lt;/span&gt;
                &lt;span class="na"&gt;encoding&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;utf-8&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                &lt;span class="na"&gt;flag&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;w&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="p"&gt;},&lt;/span&gt;
        &lt;span class="p"&gt;);&lt;/span&gt;
   &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;pythonProcess&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;spawnSync&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;python3&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
     &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;/usr/src/app/scripts/python-script.py&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
     &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;first_function&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
     &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;/usr/src/app/scripts/args.json&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
     &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;/usr/src/app/scripts/results.json&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;
   &lt;span class="p"&gt;]);&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;pythonProcess&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;stdout&lt;/span&gt;&lt;span class="p"&gt;?.&lt;/span&gt;&lt;span class="nx"&gt;toString&lt;/span&gt;&lt;span class="p"&gt;()?.&lt;/span&gt;&lt;span class="nx"&gt;trim&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;error&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;pythonProcess&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;stderr&lt;/span&gt;&lt;span class="p"&gt;?.&lt;/span&gt;&lt;span class="nx"&gt;toString&lt;/span&gt;&lt;span class="p"&gt;()?.&lt;/span&gt;&lt;span class="nx"&gt;trim&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

   &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;status&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;result&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;OK&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;status&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;buffer&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;readFile&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;/usr/src/app/scripts/results.json&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;resultParsed&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;JSON&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;parse&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;buffer&lt;/span&gt;&lt;span class="p"&gt;?.&lt;/span&gt;&lt;span class="nx"&gt;toString&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt;
    &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;send&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;resultParsed&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;toString&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;send&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;JSON&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;stringify&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="na"&gt;status&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;500&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;message&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Server error&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="p"&gt;}))&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;

&lt;span class="c1"&gt;//Creates the server on default port 8000 and can be accessed through localhost:8000&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;port&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;8000&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;listen&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;port&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`Server is listening on PORT &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;port&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The first parameter, that was passed to the &lt;code&gt;spawnSync&lt;/code&gt; is &lt;code&gt;command&amp;lt;string&amp;gt;&lt;/code&gt;(the command to run, in this case is &lt;code&gt;python3&lt;/code&gt; - the version of python that is required to run your libraries), the second is &lt;code&gt;args &amp;lt;string[]&amp;gt;&lt;/code&gt;(list of string arguments). You can also pass the third parameter &lt;code&gt;options&lt;/code&gt;, the more detailed information can be found by the &lt;a href="https://nodejs.org/api/child_process.html#child_processspawnsynccommand-args-options"&gt;link&lt;/a&gt;.&lt;br&gt;
I propose to consider in more detail the list I am passing as the second argument&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;'/usr/src/app/scripts/python-script.py' - is the path of the script I need to execute&lt;/li&gt;
&lt;li&gt;'first_function' - is the name of the function I want to run inside that file&lt;/li&gt;
&lt;li&gt;'/usr/src/app/scripts/args.json' - is the path to the file with arguments to this function. Actually you can directly pass those arguments  using &lt;code&gt;JSON.stringify()&lt;/code&gt; function in Node.js, but as for me  it's better to write those arguments down into the file and then read that from Python script. One of the reasons is that you might need to pass as arguments large arrays of data and this will cause errors.&lt;/li&gt;
&lt;li&gt;'/usr/src/app/scripts/results.json' - is the path, where I want my Python script to write results, after it is finished, so I can read that from node.js.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Passed arguments can be found in &lt;code&gt;sys.argv&lt;/code&gt; array in Python script. The &lt;code&gt;sys.argv&lt;/code&gt; list is a list that contains command line arguments in a python program. Whenever we run a python program from a command line interface, we can pass different arguments to the program. The program stores all the arguments and the file name of the python file in the sys.argv list. This can be seen in more detail by writing a simple example script&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;json&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;sys&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;first_function&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt;
    &lt;span class="n"&gt;json_obj&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;open&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;sys&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;argv&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;
    &lt;span class="n"&gt;data&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;json&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;load&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;json_obj&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;calculatedResults&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="c1"&gt;# it is just example of data to return, in fact you will calculate it bellow
&lt;/span&gt;    &lt;span class="n"&gt;X&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s"&gt;"X"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
    &lt;span class="n"&gt;y&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s"&gt;"y"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
    &lt;span class="c1"&gt;# do some your calculations based on X and y and put the result to the calculatedResults
&lt;/span&gt;    &lt;span class="c1"&gt;# print(make_pipeline)
&lt;/span&gt;    &lt;span class="n"&gt;json_object_result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;json&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;dumps&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;calculatedResults&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;indent&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="k"&gt;with&lt;/span&gt; &lt;span class="nb"&gt;open&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;sys&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;argv&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="s"&gt;"w"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;outfile&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="n"&gt;outfile&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;write&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;json_object_result&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"OK"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;


&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;sys&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;argv&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="s"&gt;'first_function'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;first_function&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;

&lt;span class="n"&gt;sys&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;stdout&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;flush&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In two words, in Node.js script we write down to the file all required arguments, run &lt;code&gt;spawnSync&lt;/code&gt; passing list of arguments and after Python script reads passed arguments from the file, makes all calculations and writes down to the file all results. At the moments all this results can be read in Node.js from file.&lt;/p&gt;

&lt;h2&gt;
  
  
  Run Python script, that uses specific Python libraries
&lt;/h2&gt;

&lt;p&gt;One of the main goals is to have ability to use built-in AI or other libraries. It's always not a good idea to install dependencies and libraries globally in a system, it can lead to version conflicts, deployment difficulties, and problems with multiple developers working on a project at the same time, for example. That's why the perfect way is to use Docker. You can build the image yourself, but the easiest solution is to use an existing one.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight docker"&gt;&lt;code&gt;&lt;span class="k"&gt;FROM&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s"&gt;nikolaik/python-nodejs:latest&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="k"&gt;as&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s"&gt;development&lt;/span&gt;
&lt;span class="k"&gt;WORKDIR&lt;/span&gt;&lt;span class="s"&gt; /usr/src/app&lt;/span&gt;
&lt;span class="k"&gt;RUN &lt;/span&gt;pip &lt;span class="nb"&gt;install &lt;/span&gt;scikit-learn numpy pandas simplejson
&lt;span class="k"&gt;COPY&lt;/span&gt;&lt;span class="s"&gt; ./package.json ./yarn.lock ./&lt;/span&gt;
&lt;span class="k"&gt;RUN &lt;/span&gt;yarn
&lt;span class="k"&gt;RUN &lt;/span&gt;yarn &lt;span class="nb"&gt;install&lt;/span&gt;
&lt;span class="k"&gt;COPY&lt;/span&gt;&lt;span class="s"&gt; ./ ./&lt;/span&gt;
&lt;span class="k"&gt;RUN &lt;/span&gt;yarn build
&lt;span class="k"&gt;ENV&lt;/span&gt;&lt;span class="s"&gt; PORT 4000&lt;/span&gt;
&lt;span class="k"&gt;EXPOSE&lt;/span&gt;&lt;span class="s"&gt; $PORT&lt;/span&gt;


&lt;span class="c"&gt;# Production mode&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s"&gt;nikolaik/python-nodejs:latest&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="k"&gt;as&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s"&gt;production&lt;/span&gt;
&lt;span class="k"&gt;RUN &lt;/span&gt;pip &lt;span class="nb"&gt;install &lt;/span&gt;scikit-learn numpy pandas simplejson
&lt;span class="k"&gt;ARG&lt;/span&gt;&lt;span class="s"&gt; NODE_ENV=production&lt;/span&gt;
&lt;span class="k"&gt;ENV&lt;/span&gt;&lt;span class="s"&gt; NODE_ENV=${NODE_ENV}&lt;/span&gt;
&lt;span class="k"&gt;WORKDIR&lt;/span&gt;&lt;span class="s"&gt; /usr/src/app&lt;/span&gt;
&lt;span class="k"&gt;COPY&lt;/span&gt;&lt;span class="s"&gt; --from=development /usr/src/app ./&lt;/span&gt;
&lt;span class="k"&gt;ENV&lt;/span&gt;&lt;span class="s"&gt; PORT 4000&lt;/span&gt;
&lt;span class="k"&gt;EXPOSE&lt;/span&gt;&lt;span class="s"&gt; $PORT&lt;/span&gt;
&lt;span class="k"&gt;CMD&lt;/span&gt;&lt;span class="s"&gt; ["node", "dist/main"]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;For now we can use &lt;code&gt;scikit-learn&lt;/code&gt;, &lt;code&gt;numpy&lt;/code&gt;, &lt;code&gt;pandas&lt;/code&gt; and &lt;code&gt;simplejson&lt;/code&gt; libraries on Python script, as they are available in the created docker container.&lt;/p&gt;

&lt;p&gt;By following the steps above, you can create a new process and execute a Python script from your Node.js application. This can be useful in cases where you need to integrate Python code into your Node.js application or when you need to perform certain tasks that are better suited for Python. Described simple app can be downloaded from the &lt;a href="https://github.com/liliya-dev/python-scripts-node.js"&gt;Github&lt;/a&gt;, there you can also find instructions how to run it.&lt;/p&gt;

&lt;p&gt;Happy coding and wish you to have a nice day!&lt;/p&gt;

</description>
      <category>python</category>
      <category>node</category>
      <category>javascript</category>
    </item>
    <item>
      <title>Part 2. Quick guide to make your Azure applications work.</title>
      <dc:creator>Chuiko Liliia</dc:creator>
      <pubDate>Wed, 05 Jan 2022 12:58:11 +0000</pubDate>
      <link>https://dev.to/halolab/part-2-quick-guide-to-make-your-azure-applications-work-gd1</link>
      <guid>https://dev.to/halolab/part-2-quick-guide-to-make-your-azure-applications-work-gd1</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;In the previous article, we have already created an Azure App service instance. And now you are able to see the Microsoft Azure default page by the link. This time we are going to migrate Strapi app, Next.js app and Mongo DB to Azure. &lt;/p&gt;

&lt;h2&gt;
  
  
  Preparing Azure App Services for the Migration process
&lt;/h2&gt;

&lt;p&gt;Your project probably has specific environment variables. They should be added to Azure.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Go to the Azure App service instance, find the Configuration in the Settings tab, select Application Settings section.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Here are two kinds of it. Application Settings and Connection Strings. There is only one case for using strings instead of settings, more detailed information can be found by the &lt;a href="https://docs.microsoft.com/en-us/azure/app-service/configure-common" rel="noopener noreferrer"&gt;link&lt;/a&gt;. Use Application settings and click on &lt;em&gt;&lt;strong&gt;+New application setting&lt;/strong&gt;&lt;/em&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Put the name and value of the variable and select if it is a deployment slot setting. With this setting in place, database connection strings and other settings are not swapped when the slots are swapped. So the staging slot will always point to the staging database, and the production slot will always point to the production database.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fg0oj5jkik3yx6bx1fdz6.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fg0oj5jkik3yx6bx1fdz6.jpeg" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Keep in mind that the values you set in App Service override the values in Web.config and others.&lt;/p&gt;

&lt;p&gt;Later I will talk about different deployment methods. But no matter which one you choose, you need to know that deploying new code directly to the production application is a bad practice. For this purpose, every App service instance has staging slots. Deploying your application to a non-production slot has the following benefits:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;You can validate app changes in a staging deployment slot before swapping it with the production slot.&lt;/li&gt;
&lt;li&gt;Deploying an app to a slot first and swapping it into production guarantees that all instances of the slot are warmed up before being swapped into production. This eliminates downtime when you deploy your app. The traffic redirection is seamless, and no requests are dropped because of swap operations. You can automate this entire workflow by configuring autoswap when pre-swap validation isn't needed.&lt;/li&gt;
&lt;li&gt;After a swap, the slot with the previously staged app now has the previous production app. If the changes swapped into the production slot aren't as you expect, you can perform the same swap immediately to get your "last known good site" back.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;So the best idea is to create a preview slot that will not affect the production. Creating the slot for an app:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Go to Azure App service instance, find the deployment slots in the Deployment tab, click on &lt;strong&gt;&lt;em&gt;Add slot&lt;/em&gt;&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;Put the slot name. It will take your main slot root domain name + name that you will write.&lt;/li&gt;
&lt;li&gt;After that, you can select if you need to clone all settings from the main slot or not. If you clone settings, it will make the full copy of the existing slot with the same environment variables. So if you want to use another database or change other settings, you shouldn't clone settings or change them manually after the new slot is ready.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F6smg4u4ja5qy97nqdipx.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F6smg4u4ja5qy97nqdipx.jpeg" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In a few minutes, the preview slot will be available by its own url and can be found in the Deployment Slots tab of the Azure App service instance.  And we can proceed to deploy.&lt;/p&gt;

&lt;h2&gt;
  
  
  Choosing the deployment way
&lt;/h2&gt;

&lt;p&gt;There are a few options to move your project to the instance. Most popular are:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;You can deploy to Azure App Service as part of your continuous deployment (CD) workflows via GitHub, Azure Repos&lt;/li&gt;
&lt;li&gt;Try Azure Repos with Azure App, make sure you have already created an Azure DevOps Organization to store your project files.&lt;/li&gt;
&lt;li&gt;Or use the Azure App Service extension for VS Code to create quickly, manage, and deploy your websites. Just press the &lt;strong&gt;&lt;em&gt;install button&lt;/em&gt;&lt;/strong&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fbct94qnzxfnswjqh74pn.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fbct94qnzxfnswjqh74pn.jpg" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The simplest way for me is deploying via VScode. You will be able to create and deploy resources with it. But this method has a list of disadvantages for my purposes and for. Probably this deployment way will be described in my other article, and for now, deploying using workflows via GitHub was chosen for my project. &lt;/p&gt;

&lt;h2&gt;
  
  
  Steps to deploy my Strapi back-end application (or any other node.js app) to Azure via GitHub:
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Certainly, to achieve this target, your app should be connected to the remote git repository. &lt;/li&gt;
&lt;li&gt;Go to your project and create the &lt;em&gt;.github&lt;/em&gt; folder at the root of it.&lt;/li&gt;
&lt;li&gt;In the &lt;em&gt;.github&lt;/em&gt; folder create &lt;em&gt;workflows&lt;/em&gt; folder.&lt;/li&gt;
&lt;li&gt;In &lt;em&gt;workflows&lt;/em&gt; folder create the file  with the following name scheme 
&lt;em&gt;branch_name_app_name(slot_name).yml&lt;/em&gt;
&lt;/li&gt;
&lt;li&gt;For example:
App-name: ‘production-app’
Branch from which you want deploy: master
Slot: ‘preview’
Final name of the file: master_production-app(preview).yml&lt;/li&gt;
&lt;li&gt;Now configure the created &lt;em&gt;.yml&lt;/em&gt; file with the following settings. Here is an example that uses the above-declared names:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  push:
    branches:
      - master
  pull_request:
    branches:
      - master

env:
  AZURE_WEBAPP_NAME: production-app  # set this to your application's name
  AZURE_WEBAPP_PACKAGE_PATH: "."      # set this to the path to your web app project, defaults to the repository root
  NODE_VERSION: '14.x'                # set this to the node version to use
  API_URL: "https://production-app.azurewebsites.net" # main slot url

jobs:
  build-and-deploy:
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@master
    - name: Set Node.js version
      uses: actions/setup-node@v1
      with:
        node-version: '14.x'

    # install dependencies, build, and test
    - name: npm install, build, and test  #describes which commands need to be run
      run: |
        npm install
        npm run build --if-present
        npm run test --if-present

    - uses: azure/webapps-deploy@v2
      with:
        app-name: 'production-app’'
        slot-name: 'preview'
        publish-profile: ${{ secrets.AzureAppService_PublishProfile_PRODUCTION }}
        package: .

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;In the file, all seems to be clear. The only exception is some odd variable used on the penultimate line of code. This setting is required to secure your deployments. Reproduce the following steps to get the value :&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;a.&lt;/strong&gt; Go to your preview slot instance and select Get Publish Profile. The file will be downloaded. Open it with the notepad or use your imagination.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fh3trnoqh02xwzbv2jugy.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fh3trnoqh02xwzbv2jugy.jpeg" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;b.&lt;/strong&gt; Copy all content of the opened file.&lt;br&gt;
&lt;strong&gt;c.&lt;/strong&gt; Go to your repository on GitHub -&amp;gt; Settings -&amp;gt; Secrets and press &lt;strong&gt;&lt;em&gt;New Repository Secret button&lt;/em&gt;&lt;/strong&gt;.&lt;br&gt;
&lt;strong&gt;d.&lt;/strong&gt; Add name &lt;em&gt;AZUREAPPSERVICE_PUBLISHPROFILE_PRODUCTION&lt;/em&gt; in value input, paste the copied content from the file and save changes. In two words, this variable connects your repo with the application instance on Azure.&lt;/p&gt;

&lt;p&gt;So, when this thousandth step is complete, you can finally push your code to GitHub by directly pushing or creating a pull request to master-branch. Now it is time to take a cup of tea and just follow the action with full deployment logs on GitHub in Actions tab. It may take some time…&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F6om0667eejx6da84en26.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F6om0667eejx6da84en26.jpeg" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;What happens now? First of all, GitHub sets the node.js version, installs all dependencies from package.json, and runs the build command. If there are no errors during building, it zips all the files without the node-modules folder and passes them to Azure. Azure does the same steps: extracts files, installs node modules, creates the production build, then removes the previous project folder and instead puts the new one. After it starts the new Docker container and it is warmed up, it runs the npm start command and starts the app.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fq9ne1dol03wx3k2taqay.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fq9ne1dol03wx3k2taqay.jpg" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Don’t be nervous while waiting. The action can take up to 10 minutes or even more for the first time deploying. When the action is completed, you can visit the link of the preview slot. If you are lucky, you will see your app working. But don’t forget the main Microsoft concept: Microsoft — Just restart it. Check if everything works, and then proceed to swap the app to the production slot. To do this, go to the production Azure App service to Deployment slots, find and press the &lt;em&gt;&lt;strong&gt;swap button&lt;/strong&gt;&lt;/em&gt;. Select which slot to swap and to which, confirm the action and wait about 5 minutes. When the swap process is ready, you will get the message. And now all your changes are available on the production slot, and the preview slot link should lead you to the example Microsoft Page.&lt;/p&gt;

&lt;h2&gt;
  
  
  Steps to deploy my Next.js application to Azure via GitHub.
&lt;/h2&gt;

&lt;p&gt;Actually, the steps to deploy next.js are almost the same as for Node.js application. There are some small distinctions. Next.js project doesn’t have the obvious server.js file that is required to deploy it to Azure App Service. There is also another way: deploy to Static Web App Azure service. But it also can’t be used in a simple way because Next.js has the server-side part, which leads to difficulties with deploying it as the static app. So here is what we should do to deploy Next.js app to Azure App Service, besides what we have already done for the previous app:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;In the root project folder create &lt;em&gt;server.js&lt;/em&gt; file with the next content and install all missing dependencies:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const { createServer } = require("http");
const next = require("next");

const port = process.env.PORT || 3000;
const app = next({ dev: false });
const handle = app.getRequestHandler();

app.prepare().then(() =&amp;gt; {
  createServer((req, res) =&amp;gt; {
    handle(req, res); 
  }).listen(port, (err) =&amp;gt; {
    if (err) throw err;
    console.log(`&amp;gt; Ready on http://localhost:${port}`);
  });
});

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Change your &lt;strong&gt;start&lt;/strong&gt; script in the &lt;em&gt;package.json&lt;/em&gt; file to “node server”.&lt;/li&gt;
&lt;li&gt;In the projects root create &lt;em&gt;web.config&lt;/em&gt; file with the code:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;?xml version="1.0" encoding="utf-8"?&amp;gt;
&amp;lt;!--
     This configuration file is required if iisnode is used to run node processes behind
     IIS or IIS Express.  For more information, visit:
     https://github.com/tjanczuk/iisnode/blob/master/src/samples/configuration/web.config
--&amp;gt;

&amp;lt;configuration&amp;gt;
  &amp;lt;system.webServer&amp;gt;
    &amp;lt;!-- Visit http://blogs.msdn.com/b/windowsazure/archive/2013/11/14/introduction-to-websockets-on-windows-azure-web-sites.aspx for more information on WebSocket support --&amp;gt;
    &amp;lt;webSocket enabled="false" /&amp;gt;
    &amp;lt;handlers&amp;gt;
      &amp;lt;!-- Indicates that the server.js file is a node.js site to be handled by the iisnode module --&amp;gt;
      &amp;lt;add name="iisnode" path="server.js" verb="*" modules="iisnode"/&amp;gt;
    &amp;lt;/handlers&amp;gt;
    &amp;lt;rewrite&amp;gt;
      &amp;lt;rules&amp;gt;
        &amp;lt;!-- Do not interfere with requests for node-inspector debugging --&amp;gt;
        &amp;lt;rule name="NodeInspector" patternSyntax="ECMAScript" stopProcessing="true"&amp;gt;
          &amp;lt;match url="^server.js\/debug[\/]?" /&amp;gt;
        &amp;lt;/rule&amp;gt;

        &amp;lt;!-- First we consider whether the incoming URL matches a physical file in the /public folder --&amp;gt;
        &amp;lt;rule name="StaticContent"&amp;gt;
          &amp;lt;action type="Rewrite" url="public{REQUEST_URI}"/&amp;gt;
        &amp;lt;/rule&amp;gt;

        &amp;lt;!-- All other URLs are mapped to the node.js site entry point --&amp;gt;
        &amp;lt;rule name="DynamicContent"&amp;gt;
          &amp;lt;conditions&amp;gt;
            &amp;lt;add input="{REQUEST_FILENAME}" matchType="IsFile" negate="True"/&amp;gt;
          &amp;lt;/conditions&amp;gt;
          &amp;lt;action type="Rewrite" url="server.js"/&amp;gt;
        &amp;lt;/rule&amp;gt;
      &amp;lt;/rules&amp;gt;
    &amp;lt;/rewrite&amp;gt;

    &amp;lt;!-- 'bin' directory has no special meaning in node.js and apps can be placed in it --&amp;gt;
    &amp;lt;security&amp;gt;
      &amp;lt;requestFiltering&amp;gt;
        &amp;lt;hiddenSegments&amp;gt;
          &amp;lt;remove segment="bin"/&amp;gt;
        &amp;lt;/hiddenSegments&amp;gt;
      &amp;lt;/requestFiltering&amp;gt;
    &amp;lt;/security&amp;gt;

    &amp;lt;!-- Make sure error responses are left untouched --&amp;gt;
    &amp;lt;httpErrors existingResponse="PassThrough" /&amp;gt;

    &amp;lt;!--
      You can control how Node is hosted within IIS using the following options:
        * watchedFiles: semi-colon separated list of files that will be watched for changes to restart the server
        * node_env: will be propagated to node as NODE_ENV environment variable
        * debuggingEnabled - controls whether the built-in debugger is enabled
      See https://github.com/tjanczuk/iisnode/blob/master/src/samples/configuration/web.config for a full list of options
    --&amp;gt;
    &amp;lt;iisnode watchedFiles="web.config;*.js"/&amp;gt;
  &amp;lt;/system.webServer&amp;gt;
&amp;lt;/configuration&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Reproduce all steps for deploying node.js app.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Congrats! Deploying both front-end and back-end applications is finally finished. The last thing we need to migrate is the Mongo database.&lt;/p&gt;

&lt;p&gt;The first and main thing you should know before starting the migration database to &lt;strong&gt;Azure Cosmos DB&lt;/strong&gt; for &lt;strong&gt;Mongo&lt;/strong&gt; – don’t ever do this!!! &lt;strong&gt;Strapi&lt;/strong&gt; and &lt;strong&gt;Azure Cosmos DB for Mongo&lt;/strong&gt; are almost incompatible. Actually, I still don’t understand what Azure Cosmos for Mongo is compatible with. &lt;/p&gt;

&lt;p&gt;It doesn’t support a lot of operators and expressions that Strapi uses. For example: &lt;strong&gt;&lt;em&gt;$let&lt;/em&gt;&lt;/strong&gt;, &lt;strong&gt;&lt;em&gt;$where&lt;/em&gt;&lt;/strong&gt;, &lt;em&gt;&lt;strong&gt;$meta&lt;/strong&gt;&lt;/em&gt; and others. I have made a migration and faced indexing problems and a lot of unsupported operators. After a huge amount of work was done and after most of the problems were fixed, it finally started to work together. But when I updated the Strapi to the new version, I got one thousand additional problems. And finally, official representatives of the company Strapi gave me the answer:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Technically CosmosDB is not entirely MongoDB (even with the API implemented) It's not on the list of supported databases, and may not fully work with the ORM being used (mongoose).&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Moreover, it has a complicated way to take backups. The system takes snapshots, but you can't get them immediately. Firstly you need to create a support request for the Microsoft Support team. It can be done manually in 2 minutes in Mongo Atlas. Just select the required snapshot and click the &lt;strong&gt;&lt;em&gt;restore button&lt;/em&gt;&lt;/strong&gt;. Looks much simpler, doesn't it?&lt;/p&gt;

&lt;p&gt;Don't waste time fixing problems, there is an easy way to avoid them. If your target is just to speed up the application, use Mongo Atlas for hosting the database and select the Azure cloud provider and the region closest to your customers. In case you need more options for regions, backup settings and some other benefits, learn about dedicated cluster tier features.&lt;/p&gt;

&lt;p&gt;If Mongo Atlas cluster is hosted on Azure servers, most of the requests will not go over the public network, they will use the Azure backbone network instead. Which is more secure and the fastest way to deliver the data.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fdoio4zdi4f48h2dpfwo0.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fdoio4zdi4f48h2dpfwo0.jpg" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In the end, we have two applications moved to Azure and Mongo DB hosted on the Mongo Atlas. It is the optimal solution for my purposes. But be ready to read more about Azure, believe me, you will definitely need it.&lt;/p&gt;

</description>
      <category>azure</category>
      <category>nextjs</category>
      <category>mongodb</category>
      <category>node</category>
    </item>
    <item>
      <title>From zero to one. How to Deploy your MongoDB, Node, Strapi, NextJS app to Microsoft Azure.</title>
      <dc:creator>Chuiko Liliia</dc:creator>
      <pubDate>Wed, 29 Dec 2021 15:18:26 +0000</pubDate>
      <link>https://dev.to/halolab/from-zero-to-one-how-to-deploy-your-mongodb-node-strapi-nextjs-app-to-microsoft-azure-4c7g</link>
      <guid>https://dev.to/halolab/from-zero-to-one-how-to-deploy-your-mongodb-node-strapi-nextjs-app-to-microsoft-azure-4c7g</guid>
      <description>&lt;h2&gt;
  
  
  Part 1. Introduction and creating all required for migration resources.
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Introduction&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Hi to everyone! This article will be helpful if you have little or no knowledge of cloud technologies.&lt;/p&gt;

&lt;p&gt;There are three main cloud providers: Google Cloud, AWS and Azure. Let’s imagine that your task is to move the application to Azure.&lt;/p&gt;

&lt;p&gt;Before continuing, note that the best way to familiarize with basic Azure cloud features, to gain a foundational knowledge of cloud services, and the way those services are provided with Microsoft Azure is to learn and pass &lt;strong&gt;&lt;em&gt;Exam AZ-900: Microsoft Azure Fundamentals&lt;/em&gt;&lt;/strong&gt;. The exam is for candidates who are just beginning to work with cloud-based solutions and services or are new to Azure. Azure Fundamentals can be used to prepare for other Azure role-based or specialty certifications, but it is not a prerequisite for any of them. All Microsoft Azure learning has two options: online, which is always free, and instructor-led, which is paid. Passing the exam and getting the certification is always paid. The price depends on the country. One more tip: learn it before migration because learning during the process is a bad idea. There is much to say about passing Microsoft exams, but I suggest we move on to the next step and explore the project structure. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Project structure and reasons for migration to Azure&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;So, let’s do a deep dive into the project structure before moving on.&lt;/p&gt;

&lt;p&gt;Front-end: NEXT.js app, hosted on Vercel.&lt;br&gt;
Back end: Strapi CMS + custom node scripts, hosted on Heroku&lt;br&gt;
DB: Mongo db hosted on Mongo Atlas&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The common problems with current providers include:&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;  No simple ways to spread application to multiply regions and elastically scale the capacity based on demand.&lt;/li&gt;
&lt;li&gt;  No static inbound and outbound IP addresses (only via proxy), which I needed to secure back-end and DB connections.&lt;/li&gt;
&lt;li&gt;  No obvious ways to give separate accesses to resources, based on roles.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;First steps to start to work with Azure, regardless wich computing services you choose to host applications&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Firstly go to &lt;a href="https://azure.microsoft.com/" rel="noopener noreferrer"&gt;Microsoft Azure Portal&lt;/a&gt; and create the account. After you complete the registration, you will have a single tenant ID associated with your account, which will not change unless you ask Microsoft to delete your account. &lt;strong&gt;&lt;em&gt;Tenant&lt;/em&gt;&lt;/strong&gt; is associated with a single identity (person, company, or organization) and can own one or several &lt;strong&gt;&lt;em&gt;subscriptions&lt;/em&gt;&lt;/strong&gt;. At this step you will only have one subscription. An Azure subscription is a logical container used to provision resources in Azure. It holds the details of all your resources like virtual machines (VMs), databases, and more. When you create an Azure resource like a VM, you identify the subscription it belongs to. A subscription is linked to a payment setup and each subscription will result in a separate bill and can use different payment methods. Resources from one subscription are isolated from resources in other subscriptions.&lt;/p&gt;

&lt;p&gt;During the registration in Azure you may choose to get 200$ dollars on balance of the default subscription that you can spend on Azure services. It is a very useful option for beginners in cloud technologies to test different options.&lt;/p&gt;

&lt;p&gt;Now you are signed in, have a tenant and one subscription inside it. The next step is to decide which Azure services to use for your apps hosting.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Choose the appropriate Azure services&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Azure offers three main cloud computing platform services: SaaS (Software as a Service), IaaS (Infrastructure as a Service), PaaS (Platform as a Service). The main differences and maintaining levels are displayed on the diagram below: &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fuhcnvgel49ek6t0rrfhx.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fuhcnvgel49ek6t0rrfhx.jpeg" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If you don’t want to be responsible for the operating system, data, runtime and middleware, but still need to manage and configure the application, the PaaS cloud computing service is a good choice. &lt;/p&gt;

&lt;p&gt;So, PaaS — it is where Azure provider hosts the hardware and software on its own infrastructure and delivers this platform to the user as an integrated solution, solution stack, or service through an internet connection, while the developer is allowed to develop, run, and manage his own apps without having to build and maintain the infrastructure or platform usually associated with the process. &lt;/p&gt;

&lt;p&gt;When we have decided which cloud computing service to use, let's proceed to the next step.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Creating required resources&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The next step is to create the App Service Plan and the Resource Group.&lt;/p&gt;

&lt;p&gt;An &lt;strong&gt;&lt;em&gt;App Service Plan&lt;/em&gt;&lt;/strong&gt;, in two words, at a very high level is just the container in which your web applications run. It is used to determine the resources available to your application (or applications) and their boundary. Comparing this to an on premises environment, the app service environment is one server or servers on which your application is deployed. The app service plan defines what configuration of hardware your app runs on and how many servers you have. You can deploy multiple web applications inside the same App Service Plan. You do not need a separate web app plan for each web app.&lt;/p&gt;

&lt;p&gt;When you create an App Service plan in a certain region (for example, Western Europe), a set of compute resources is created for that plan in that region. Whatever apps you put into this App Service plan run on these compute resources as defined by your App Service plan.&lt;/p&gt;

&lt;p&gt;A &lt;strong&gt;&lt;em&gt;resource group&lt;/em&gt;&lt;/strong&gt; is a logical container into which Azure resources like web apps, databases, and storage accounts are deployed and managed. The Resource Group can include all the resources for the solution, or only those resources that you want to manage as a group. You decide how to allocate resources to resource groups based on what makes the most sense for your organization. Generally, add resources that share the same lifecycle to the same resource group so you can easily deploy, update, and delete them as a group.&lt;/p&gt;

&lt;p&gt;Steps to create App Service Plan with Resource Group simultaneously:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Type App Service Plan in the search bar, select it from the dropdown and click &lt;em&gt;+Create new.&lt;/em&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fnxwoizxozmxxqhzstntf.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fnxwoizxozmxxqhzstntf.jpg" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Now you need to configure it. As you don’t have the Resource Group yet, press create new, enter its name, and it will be created with the App Service Plan and placed inside it. Some other tips: &lt;/li&gt;
&lt;li&gt;Enter the App Service Plan name that will clearly describe group of resources inside it (for example, name of the project or of the group of projects)
select the region (the better option is to choose the closest region to your customers' location because it will affect the latency)&lt;/li&gt;
&lt;li&gt;Select operating system to run your apps, in this case Linux was selected &lt;/li&gt;
&lt;li&gt;Select the pricing tier. You can choose from dev/test or production plans. But in case you want to configure automatic deployment from GitHub, choose the P1V2 or higher pricing tier. The application plans less than P1V2 have a list of limitations. The next features are not available:&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;a) &lt;em&gt;Auto-scale feature&lt;/em&gt;. Autoscale is a built-in feature of Cloud Services, Mobile Services, Virtual Machine Scale Sets, and Websites that helps applications perform their best when demand changes.&lt;br&gt;
b)   &lt;em&gt;Daily backups.&lt;/em&gt; Here, probably, nothing to explain.&lt;br&gt;
c)   &lt;em&gt;Traffic manager&lt;/em&gt; improves performance and availability by routing traffic between multiple instances of your app.&lt;br&gt;
d)   &lt;em&gt;Staging slots&lt;/em&gt;. Useful for testing and deployments before swapping them into production.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;If you want to create a high volume of resources in the future, use tags. Azure tags are name-value pairs that are used to organize resources in Azure Portal. You can apply tags for individual resources or tag the resource group that they are part of.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F5stgnl1wrkeey82rmaru.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F5stgnl1wrkeey82rmaru.jpg" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Press Review and create. It can take up to 10 minutes.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;App Service Plan and the Resource Group are ready to place resources inside. That’s why we can proceed to the next steps.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Creating instances for applications&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;As PaaS was selected, we should search among its services. The appropriate service is App Service. It enables you to build and host web apps, mobile back ends, and RESTful APIs in the programming language of your choice without managing infrastructure. It offers auto-scaling and high availability, supports both Windows and Linux, and enables automated deployments from GitHub, Azure DevOps, or any Git repo.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;To create the App Service find this category in the search bar and press +Create. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;You will be redirected to this service configuration page. You need to select from the dropdown the subscription on the resource group, where it needs to be located. Put the name of the application (for example my-project-back-end-development).&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;In the Publish tab select Code option if you are going to just put the code of your project instead of the ready Docker container. It will make Azure create the Docker container for you itself and run your application inside it.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Select Node from the dropdown of runtime stack. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The application region and operating system will be chosen automatically, according to your App Service Plan settings. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Press next and in the Monitoring section I fully recommend you to switch on Application Insights. It allows you to automatically monitor your application. It will automatically detect performance anomalies and includes powerful analytics tools to diagnose issues and to understand how users actually use your app. It's designed to help you continuously improve performance and usability. It works for apps on various platforms, including .NET, Node.js, Java, and Python hosted on-premises, hybrid, or any public cloud. It integrates with your DevOps process and has connection points to various development tools. All other settings, at this moment, let's keep by default. Click create and review, check if all settings are correct, and confirm. It can take up to 5-10 minutes.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fca7ddcye3khplbificzr.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fca7ddcye3khplbificzr.jpg" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The instance for hosting the back-end is ready, and you can reproduce previous steps to create a separate App Service instance for the front-end application. Once it is ready we go to the next part.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Creating instances for database&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;As mentioned, MongoDB is used in the project. Azure has an opportunity to place MongoDB using the Azure Cosmos DB's API for MongoDB. It has some limitations. With the full list of supported operators and limitations or exceptions you can familiarize by the &lt;a href="https://docs.microsoft.com/en-us/azure/cosmos-db/mongodb/feature-support-40" rel="noopener noreferrer"&gt;link&lt;/a&gt;; To create DB go to Azure Cosmos DB Resource and click on Create Azure Cosmos DB account. On the next screen select Azure Cosmos DB API for MongoDB. &lt;/p&gt;

&lt;p&gt;The instance for hosting the database is ready, and you As on previous steps, select subscription and resource group, enter the database name. Select the location closest to the rest of your resources and customers to decrease the requests time. Select the Mongo version.&lt;/p&gt;

&lt;p&gt;Choosing the capacity mode of the instance is an important step to save your money. You need to know the differences between these two options. Provisioned has throughput at a constant rate, regardless of whether operations are being performed on our Cosmos DB account. Serverless Mode — we only use throughput when operations are performed on our Cosmos DB resources. Each of these options has pros and cons and if you want to know more, follow the &lt;a href="https://docs.microsoft.com/en-us/azure/cosmos-db/throughput-serverless" rel="noopener noreferrer"&gt;link&lt;/a&gt;. But in two words, serverless — pay only when using, provisioned — reserve capacity for the instance and pay for it all the time. I need to highlight that it is cheaper to use the serverless mode for non-highly loaded projects. Press the create button and wait until it is ready.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fr9eomsscm3dyzhowqjt4.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fr9eomsscm3dyzhowqjt4.jpg" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Summary&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Let’s check what we have at the moment. You can find all the created instances in your resource group on Azure Portal. Each of the application instances are already available with the Microsoft example page by the url, which you can find in the app essentials section. Cosmos db account is ready for moving your database into it.&lt;/p&gt;

&lt;p&gt;Now all resources are ready to deploy your applications and database. The &lt;a href="https://dev.to/halolab/part-2-quick-guide-to-make-your-azure-applications-work-gd1"&gt;next part of the article&lt;/a&gt; is going to be about the migration processes and how to connect all instances. Make sure you read both parts of the article and then proceed to work because we will talk about database migration in the next steps.&lt;/p&gt;

</description>
      <category>azure</category>
      <category>mongodb</category>
      <category>strapi</category>
      <category>nextjs</category>
    </item>
  </channel>
</rss>
