'Spawn child_process to run Python script using Nodejs not working in Heroku but works locally

Hi I am running Nodejs and I need to pass two input variables from a webpage into a Python script this all works well locally but when I deploy to heroku it looks like the python script is not being run. In the logs I see see 'child process close all stdio with code 1'

Logs:

2022-04-21T10:16:35.439296Z app[web.1]: req.body.Input_path: test value 1

2022-04-21T10:16:35.439334Z app[web.1]: req.body.Output_path: test value 2

2022-04-21T10:16:35.438866Z system[router]: at=info method=POST path="/wrapper" host="conversionnodeapp.factset.io" fwd="xxx" zone=a container=web.1 connect=8ms service=95ms status=200 bytes=0 protocol=https tls_version=tls1.3 request_id=3f4a4bc5-63b0-4804-a291-83dd991a0fb3

2022-04-21T10:16:35.466844Z app[web.1]: child process close all stdio with code 1

My index.js code:

const express = require('express')
const {spawn} = require('child_process');
const { join } = require('path');
const app = express()
const PORT = process.env.PORT || 5000

app.use(express.json())

app.use(express.static(join(__dirname, 'public')));

app.get('/', (req, res) => {
    res.sendFile(join(__dirname, "index.html"));
});



app.post('/wrapper', (req, res) => {
    console.log(`req.body.Input_path: ${req.body.Input_path}`),
    console.log(`req.body.Output_path: ${req.body.Output_path}`)
 
 var dataToSend;
 // spawn new child process to call the python script
 const python = spawn('python', ['wrapper.py']);

 python.stdout.on('data', function (data) {
  console.log('Pipe data from python script ...');
  dataToSend = data.toString();
  console.log(data)
 });

 python.on('close', (code) => {
 console.log(`child process close all stdio with code ${code}`);
 // send data to browser
 res.send(dataToSend)
 });
 
})
app.listen(PORT, () => console.log(`Example app listening on port 
${PORT}!`))

Any help would be appreciated or if there is a better way to run a Python script from a webpage passing two variables.

P.S I am working in a network support role but want to create internal tools to help the team and learn some code along the way.

Many thanks



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source