【问题标题】:Running child c executable as child process in node.js heroku app在 node.js heroku 应用程序中将子 c 可执行文件作为子进程运行
【发布时间】:2021-10-28 09:51:58
【问题描述】:

我想在 heroku 上的 node.js 中将 c 程序作为子进程运行。 在我的 app.js 中:

app.get('/extra', function (req, res) {
    const child = spawn('./a');
    child.stdin.setDefaultEncoding('utf-8');
    child.stdin.write(52 + "\n");
    child.stdin.end();

    child.stdout.on('data', (data) =>{
        const dataString = "" + data;
        res.send(dataString);
    });
});

我正在使用带有 Makefile 的 heroku c-buildpack: 全部: gcc main.c -o a.out 它记录成功,但是当我得到 /extra 应用程序失败,并且当我尝试使用 fs.readdirSync('/').forEach... 列出所有文件时,它只记录 app.js

【问题讨论】:

    标签: node.js heroku buildpack


    【解决方案1】:
    const { child } = require("child_process")
    
    child("ls", (error, stdout, stderr) => {
        if (error) {
            console.log(`[Error] ${error.message}`);
            return;
        }
        if (stderr) {
            console.log(`[Std Error] ${stderr}`);
            return;
        }
        console.log(`${stdout}`);
    });
    

    你可以试试这个,只需运行一个系统命令ls

    【讨论】:

    • 使用了不同的语法,但它起作用了,它列出了必须由 heroku 编译的 a.out,将 spawn('./a') 切换为 spawn('./a.out') 已修复它。所以它一直是语法...... xd
    • 哦,这也经常发生在我身上,顺便说一句,您可以将您的答案标记为已回答。
    【解决方案2】:

    切换

    spawn('./a') 
    

    spawn('./a.out') 
    

    修复了问题

    【讨论】:

      猜你喜欢
      • 2012-11-02
      • 1970-01-01
      • 2015-08-19
      • 2017-11-28
      • 2019-10-17
      • 1970-01-01
      • 1970-01-01
      • 2020-05-19
      • 1970-01-01
      相关资源
      最近更新 更多