-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
26 lines (24 loc) · 802 Bytes
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
const express = require('express')
const {spawn} = require('child_process');
const bodyparser = require("body-parser")
const app = express()
const port = 3000
app.use(bodyparser.json())
app.get('/', (req, res) => {
var dataToSend;
// spawn new child process to call the python script
const python = spawn('python', ['script1.py']);
// collect data from script
python.stdout.on('data', function (data) {
console.log('Pipe data from python script ...');
dataToSend = data.toString();
});
// in close event we are sure that stream from child process is closed
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}!`))