Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Not working in POST Request of backend Nodejs application.(MERN Stack) #267

Open
Namasivaayam-L opened this issue May 24, 2022 · 0 comments
Labels

Comments

@Namasivaayam-L
Copy link

Namasivaayam-L commented May 24, 2022

The Question:
I am developing a disease prediction website. I have used react for frontend and express, mongodb, nodejs for backend. If run the python script just by executing the .js (file containing the Python-Shell code) file it works just fine. But if I run .js file in a post request in nodejs when a webpage loads in react frontend using useEffect the python script is not executing.

Any relevant python/javascript code:
JAVASCRIPT(NODEJS)


1. const {PythonShell} = require('python-shell')
2. const router = require('express').Router()
3. const spawn = require('child_process').spawn
4. router.post('/', async(req, res ) => {
5.     //METHOD -1 (Not Working)
6.     try {
7.         options = {
8.             pythonPath: 'C:\\Users\\default\\AppData\\Local\\Programs\\Python\\Python39\\',
9. 
10           args: [req.body[0]],                                //req.body[0] indicates the command line argument for the python program
11
.             scriptPath:'C:\\Users\\default\\Documents\\getSymptoms.py'  //path of my .py file
12.         };
13.         PythonShell.run('./getSymptoms.py', options, function (err, results) {
14.             if (err) throw err;
15.             return res.send(results);
16.         });
17. 
18.        //METHOD -2 (Not working too)
19.        var output
20.         const python = spawn('python.exe', ['./getSymptoms.py',req.body[0]])
21.         // PART-1 (Not working)
22.         python.stdout.on('data', function (data) {
23.             console.log('Pipe data from python script ...');
24.             output = data.toString();
25.         });
26.         //PART-2 (***WORKING***)
27.         python.on('close', (code) => {
28.             console.log(output);
29.             console.log(`child process close all stdio with code ${code}`);
30.             // send data to browser
31.             return res.send(output);
32.         });
33.         }
34.             catch (error) {
35.                 return res.send({ message: "Error" });
36.         }
37.     })
38. module.exports=router    
39. 

PYTHON FILE
print(sys.argv[1])

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant