Home > Software design >  Running child c executable as child process in node.js heroku app
Running child c executable as child process in node.js heroku app

Time:10-31

I want to run c program as child process in node.js on heroku. In my 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);
    });
});

I am using heroku c-buildpack with Makefile: all: gcc main.c -o a.out It logs successful, but when I get /extra app fails, and when I tried to list all files with fs.readdirSync('/').forEach... it only logged app.js

CodePudding user response:

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}`);
});

Can you try this, just running a system command ls.

CodePudding user response:

switching

spawn('./a') 

to

spawn('./a.out') 

fixed the issue

  • Related