<?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: Ademola Babatunde</title>
    <description>The latest articles on DEV Community by Ademola Babatunde (@addempsea).</description>
    <link>https://dev.to/addempsea</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%2F368640%2F44455e16-7487-47f5-8e16-b58f4650b421.jpeg</url>
      <title>DEV Community: Ademola Babatunde</title>
      <link>https://dev.to/addempsea</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/addempsea"/>
    <language>en</language>
    <item>
      <title>CRUD On Node FS Module; A Journey Into The World Of File Systems.</title>
      <dc:creator>Ademola Babatunde</dc:creator>
      <pubDate>Tue, 05 Oct 2021 13:42:12 +0000</pubDate>
      <link>https://dev.to/addempsea/crud-on-node-fs-module-a-journey-into-the-world-of-file-systems-3di5</link>
      <guid>https://dev.to/addempsea/crud-on-node-fs-module-a-journey-into-the-world-of-file-systems-3di5</guid>
      <description>&lt;h2&gt;
  
  
  File System in Nodejs
&lt;/h2&gt;

&lt;p&gt;The fs module in nodejs allows you to read, delete, update, and create files on your machine, it gives you access to do anything you want with the files. There are several methods within the module that allows you to do this. Allow me walk you through the process.&lt;/p&gt;

&lt;p&gt;&lt;sup&gt;The cover image was sourced from &lt;a href="https://www.digitalnamanji.com/what-is-file-system/"&gt;here&lt;/a&gt;&lt;/sup&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Must-Haves
&lt;/h2&gt;

&lt;p&gt;Obviously, you must have nodejs installed on your machine. After which you should import the fs module.&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;fs&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&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;All of the operations of the fs module can be done either synchronously or asynchronously. &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;The synchronous form blocks the Node.js event loop and further JavaScript execution until the operation is complete. Exceptions are thrown immediately and can be handled using try…catch, or can be allowed to bubble up.&lt;br&gt;
&lt;a href="https://nodejs.org/api/fs.html#fs_file_systeml"&gt;source - nodeJs doc&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Creating or Writing Files with the module
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;fs.writeFile()&lt;/code&gt; and &lt;code&gt;fs.writeFileSync()&lt;/code&gt; are the asynchronous and synchronous respectively ways of writing to a file if it already exists or creating a new file and writing to it if it does not exist. Examples of both methods are below:&lt;br&gt;
The asynchronous way,&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="nx"&gt;fs&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;writeFile&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;newfile.txt&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="s2"&gt;Hello world&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="nx"&gt;err&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="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;err&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;callback&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;err&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="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;file created successfully!&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;and the synchronous way,&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;file&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;fs&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;writeFileSync&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;newfile.txt&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="s2"&gt;Hello world&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Reading Files
&lt;/h2&gt;

&lt;p&gt;If we want to read the content of the new file we created above, this can be done using the &lt;code&gt;fs.readFile()&lt;/code&gt; and &lt;code&gt;fs.readFileSync()&lt;/code&gt; methods.  There are various encoding formats to read the file in, the default if not is specified is &lt;strong&gt;&lt;em&gt;Buffer&lt;/em&gt;&lt;/strong&gt;.  Both functions take the path to the file as the first argument, see the example below:&lt;/p&gt;

&lt;p&gt;The asynchronous way,&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="nx"&gt;fs&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="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;newfile.txt&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="nx"&gt;err&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;data&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="k"&gt;if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;err&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;throw&lt;/span&gt; &lt;span class="nx"&gt;err&lt;/span&gt;&lt;span class="p"&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 content of the file will be inside the data parameter&lt;/p&gt;

&lt;p&gt;The synchronous way,&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;data&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;fs&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;readFileSync&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;newfile.txt&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="s2"&gt;utf8&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Renaming a File
&lt;/h2&gt;

&lt;p&gt;Using the &lt;code&gt;fs.rename()&lt;/code&gt;or &lt;code&gt;fs.renameSync()&lt;/code&gt; method allows us to rename a file to any new name we want. The method takes in two arguments, the path to the file to be renamed and the second argument is the new name of the file. &lt;br&gt;
See examples below:&lt;/p&gt;

&lt;p&gt;The asynchronous way,&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="nx"&gt;fs&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;rename&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;newfile.txt&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;renamedfile.txt&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="nx"&gt;err&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="k"&gt;if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;err&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;throw&lt;/span&gt; &lt;span class="nx"&gt;err&lt;/span&gt;&lt;span class="p"&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 content of the file will be inside the data parameter&lt;/p&gt;

&lt;p&gt;The synchronous way,&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="nx"&gt;fs&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;renameSync&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;newfile.txt&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;renamedfile.txt&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Deleting a File
&lt;/h2&gt;

&lt;p&gt;To delete a file, we have the &lt;code&gt;fs.unlink()&lt;/code&gt; and &lt;code&gt;fs.unlinkSync()&lt;/code&gt;. These methods take a single argument, the path to the file to be deleted or removed. &lt;/p&gt;

&lt;p&gt;The synchronous way,&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="nx"&gt;fs&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;unlinkSync&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;renamedfile.txt&lt;/span&gt;&lt;span class="dl"&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 asynchronous way,&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="nx"&gt;fs&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;unlinkSync&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;renamedfile.txt&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="nx"&gt;err&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="k"&gt;if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;err&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;throw&lt;/span&gt; &lt;span class="nx"&gt;err&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;These are just the basic operations that can be done with the fs module on files, I only talked about files. Operations can be done on directories too and many more. For other features of the fs module, visit the &lt;a href="https://nodejs.org/api/fs.html"&gt;nodejs fs documentation&lt;/a&gt;&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>node</category>
    </item>
    <item>
      <title>Snake In The JS Shadow: Run your python script in nodeJs</title>
      <dc:creator>Ademola Babatunde</dc:creator>
      <pubDate>Sat, 04 Sep 2021 23:29:54 +0000</pubDate>
      <link>https://dev.to/addempsea/snake-in-the-js-shadow-run-your-python-script-in-nodejs-im8</link>
      <guid>https://dev.to/addempsea/snake-in-the-js-shadow-run-your-python-script-in-nodejs-im8</guid>
      <description>&lt;p&gt;&lt;sup&gt;The cover image was sourced from &lt;a href="https://pythonsway.it/javascript-python/" rel="noopener noreferrer"&gt;here&lt;/a&gt;&lt;/sup&gt;&lt;/p&gt;

&lt;p&gt;Ever wondered if you could run a python function inside a nodejs code? I wondered also and I researched and got to see a module in nodejs &lt;strong&gt;child_process&lt;/strong&gt; which allows you to run child processes. You may wonder what child processes are, well according to Wikipedia,&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;A child process in computing is a process created by another process. This technique pertains to multitasking operating systems and is sometimes called a subprocess or traditionally a subtask. There are two major procedures for creating a child process: the fork system call and the spawn.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;I will attempt to walk you through how to run a non-blocking python script inside of your nodejs code.&lt;/p&gt;

&lt;h2&gt;
  
  
  Must-Haves
&lt;/h2&gt;

&lt;p&gt;Obviously, you must have nodejs installed on your machine. After which you should import the fs module.&lt;br&gt;
Also because I want to display the results in a browser, let's use express to serve it.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;npm install express&lt;/code&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  Set up
&lt;/h2&gt;

&lt;p&gt;Create two files, &lt;strong&gt;index.js&lt;/strong&gt; and &lt;strong&gt;main.py&lt;/strong&gt;, add the code below to your index.js file&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const { spawn } = require('child_process');
const express = require("express");
const app = express();
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the main.py file, add the code below&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;print('Hello')
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&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%2Fsteemitimages.com%2FDQmP4WCzAeZaZpjjkDDy2ouW6STJkxbr7gf2SPdgA3Hrziu%2Fhr.png" 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%2Fsteemitimages.com%2FDQmP4WCzAeZaZpjjkDDy2ouW6STJkxbr7gf2SPdgA3Hrziu%2Fhr.png" alt="hr.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Main Implementation
&lt;/h2&gt;

&lt;p&gt;In the index.js file, add the following&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const pythonPromise = () =&amp;gt; {
  return new Promise((resolve, reject) =&amp;gt; {
    const python = spawn("python", ["./main.py"]);
    python.stdout.on("data", (data) =&amp;gt; {
      resolve(data.toString());
    });

    python.stderr.on("data", (data) =&amp;gt; {
      reject(data.toString());
    });
 });
};
app.get("/:name", async (req, res) =&amp;gt; {
  const dataFromPython = await pythonPromise();
  res.send(dataFromPython + req.params.name);
});
app.listen(3200, () =&amp;gt; console.log("App is running port 3200"));
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The spawn function here takes two arguments, the first &lt;code&gt;"python"&lt;/code&gt; which is the program we want to run, and &lt;code&gt;"./main"&lt;/code&gt; which is the path to the python file we wish to run.&lt;br&gt;
We all know nodejs is event-driven, the two events we are listening for are the &lt;code&gt;python.stdout.on&lt;/code&gt; and &lt;code&gt;python.stderr.on&lt;/code&gt;. The &lt;strong&gt;stderr&lt;/strong&gt; is short for standard error, which occurs while attempting to run the python script while &lt;strong&gt;stdout&lt;/strong&gt; is short for standard output, which is the return value from our script. The type of data returned is binary, which is why you need to call the &lt;code&gt;toString()&lt;/code&gt; method to convert it to a string.&lt;/p&gt;

&lt;p&gt;Start your server with &lt;code&gt;node index.js&lt;/code&gt; and visit &lt;code&gt;http://localhost:3200/yourName&lt;/code&gt; in your browser or postman to see the result.&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fsteemitimages.com%2FDQmP4WCzAeZaZpjjkDDy2ouW6STJkxbr7gf2SPdgA3Hrziu%2Fhr.png" 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%2Fsteemitimages.com%2FDQmP4WCzAeZaZpjjkDDy2ouW6STJkxbr7gf2SPdgA3Hrziu%2Fhr.png" alt="hr.png"&gt;&lt;/a&gt;&lt;br&gt;
There will really be no need to use python scripts if we are not passing arguments to the python script, let's edit our &lt;strong&gt;main.py&lt;/strong&gt; to have a function that takes arguments from your nodejs code. Replace with:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import sys
print('Hello ' + sys.argv[1] + ' your id is ' + sys.argv[2])
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Also, we add more arguments to the spawn function and our index.js file should look like this below:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const pythonPromise = (data) =&amp;gt; {
  return new Promise((resolve, reject) =&amp;gt; {
    const python = spawn("python", ["./main.py", ...data]);

    python.stdout.on("data", (data) =&amp;gt; {
      resolve(data.toString());
    });

    python.stderr.on("data", (data) =&amp;gt; {
      reject(data.toString());
    });
  });
};

app.get("/:name/:id", async (req, res) =&amp;gt; {
  const { name, id } = req.params;
  const dataFromPython = await pythonPromise([name, id]);
  res.send(dataFromPython);
});
app.listen(3200, () =&amp;gt; console.log("App is running port 3200"));
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You may be wondering what the sys module we imported in the main.py script is. According to &lt;a href="https://www.geeksforgeeks.org/how-to-use-sys-argv-in-python/" rel="noopener noreferrer"&gt;geeksforgeeks&lt;/a&gt;,&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;The sys module provides functions and variables used to manipulate different parts of the Python runtime environment. This module provides access to some variables used or maintained by the interpreter and to functions that interact strongly with the interpreter.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;And the indexes argv[1] and argv[2] are the parameters we wish to pass to the script. The first argument, argr[0] is the name of the file we are running the script from.&lt;br&gt;
Start your server with &lt;code&gt;node index.js&lt;/code&gt; and visit &lt;code&gt;http://localhost:3200/yourName/anyId&lt;/code&gt; in your browser or postman to see the result.&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%2Fsteemitimages.com%2FDQmP4WCzAeZaZpjjkDDy2ouW6STJkxbr7gf2SPdgA3Hrziu%2Fhr.png" 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%2Fsteemitimages.com%2FDQmP4WCzAeZaZpjjkDDy2ouW6STJkxbr7gf2SPdgA3Hrziu%2Fhr.png" alt="hr.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This is a very basic example to expose you to the powers of the spawn method of the child_process module. There could be complex cases like passing JSON objects to your python script or receiving JSON data from the script, but this should get you started on the path to running python scripts in your nodejs applications. ✌🏻&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>python</category>
      <category>webdev</category>
      <category>node</category>
    </item>
  </channel>
</rss>
