【问题标题】:node.js not running python script after launchnode.js 启动后未运行 python 脚本
【发布时间】:2022-07-31 06:13:25
【问题描述】:

我正在尝试制作一个可以执行 python 脚本的 node.js 服务器。这是代码

const express = require("express");
const bodyParser = require("body-parser");
const {spawn} = require('child_process');
const app = express();
const fs = require("fs");
app.use(bodyParser.urlencoded({ extended: true}));
app.use(bodyParser.json());

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

var listener = app.listen(process.env.PORT, () => {
 console.log(`App is listening on port ${listener.address().port}`);
});

app.post("/readPython", (request, response) => {
 var dataToSend;
 const python = spawn('python', ['python/cookie.py'], "Kevin");

 python.stdout.on('data', function (data) {
  dataToSend = data.toString();
});

 python.stderr.on('data', data => {
  console.error(`stderr: ${data}`);
});

 python.on('exit', (code) => {
  console.log(`child process exited with code ${code}, ${dataToSend}`);
  response.sendFile(`${__dirname}/html/result.html`);
});
});

但是每当我输入node index.js 时,它都会启动服务器,但什么也不做。有什么理由吗?我对 node.js 有点陌生

【问题讨论】:

    标签: python node.js


    【解决方案1】:

    解决这个问题的第一步是将生成的进程的stderr绑定到nodeJs的stdout

    python.stderr.pipe(process.stdout)
    

    这样你就可以得到错误信息(如果有的话)。

    就我而言,它没有将“python”识别为

    【讨论】:

      最近更新 更多