45 lines
1.2 KiB
JavaScript
45 lines
1.2 KiB
JavaScript
import { spawn } from "child_process";
|
||
export default function (req, res) {
|
||
console.log("spawnin");
|
||
//console.log(req.body);
|
||
//file
|
||
let textData = req.body.profil;
|
||
let replacedData = textData.replace("<22>", "o").replace("<22>", "e");
|
||
console.log(replacedData);
|
||
|
||
var fs = require("fs");
|
||
fs.writeFile("P.txt", replacedData, function (err) {
|
||
if (err) {
|
||
return console.error(err);
|
||
}
|
||
});
|
||
|
||
//py
|
||
let fileName = Math.floor(Math.random() * 9999) + 1000;
|
||
|
||
const python = spawn("python3", ["a.py", req.body.arguments.scale, fileName]);
|
||
|
||
let dataToSend;
|
||
python.stdout.on("data", function (data) {
|
||
console.log("Pipe data from python script ...");
|
||
dataToSend = data.toString();
|
||
console.log(dataToSend);
|
||
});
|
||
python.stderr.on("data", (data) => {
|
||
console.error(`stderr: ${data}`);
|
||
res.send("Py Error: " + data);
|
||
});
|
||
// 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
|
||
console.log(dataToSend);
|
||
console.log("done");
|
||
try {
|
||
res.status(200).send({ filename: fileName, data: dataToSend });
|
||
} catch (e) {
|
||
console.log("child process end");
|
||
}
|
||
});
|
||
}
|