英文:
Running python file using spawn in node js
问题
这是我的server.js文件:
const path = require("path");
const fs = require("fs");
const express = require("express");
const multer = require("multer");
const cors = require("cors");
const app = express();
const { spawn } = require("child_process");
const { request } = require("http");
const { response } = require("express");
const { stderr } = require("process");
function denoiseVideoByRaw(req, res) {
var spawn = require("child_process").spawn;
// 原始音频
var process = spawn("python", ["denoise_video.py"], { shell: true });
process.stdout.on("data", function async(data) {
// res.send(data.toString());
console.log(data.toString());
});
}
app.post("/denoise-video-by-raw", denoiseVideoByRaw);
app.listen(8000, () => {
console.log("server is running on localhost:8000");
});
请注意,我已经将HTML实体编码("
)替换为双引号("
),以便您的代码在运行时不会出现问题。如果您需要进一步的帮助,请告诉我。
英文:
I am trying to run python file on a post request from client(react js). Everything was working fine on the other windows 10 system. But when I tried to use it on another computer, now it is not working. On terminal, it is showing no error.
This is my server.js file:
const path = require("path");
const fs = require("fs");
const express = require("express");
const multer = require("multer");
const cors = require("cors");
const app = express();
const { spawn } = require("child_process");
const { request } = require("http");
const { response } = require("express");
const { stderr } = require("process");
function denoiseVideoByRaw(req, res) {
var spawn = require("child_process").spawn;
// raw audio
var process = spawn("python", ["denoise_video.py"], {shell: true});
process.stdout.on("data", function async(data) {
// res.send(data.toString());
console.log(data.toString());
});
}
app.post("/denoise-video-by-raw", denoiseVideoByRaw);
app.listen(8000, () => {
console.log("server is running on localhost:8000");
});
答案1
得分: 0
根据Node.js的官方文档,你还需要捕获stderr
。
请应用以下更改:
var result = '';
process.stdout.on('data', function (data) { // 回调函数无需异步
result += data.toString(); // 每个数据块都是一个 chunk
});
stderr:
process.stderr.on('data', function (data){
result += data.toString();
});
最终结果:
process.stdout.on('end',function (data){
console.log(result);
});
英文:
According to Node.js official documentation, you need to also capture stderr
.
Please apply these changes:
var result = '';
process.stdout.on('data', function (data) { // The callback function no need to be as async
result += data.toString(); // each data is a chunk
});
stderr:
process.stderr.on('data', function (data){
result += data.toString();
});
final result:
process.stdout.on('end',function (data){
console.log(result);
});
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论