I feel that your question is 2 part.
- PHP's exec equivalent for node.js
- Execute C program and provide its out put using node.js.
Sticking to pure node.js, you would be able to execute and get access to stdout and stderr with following code.
var exec = require('child_process').exec; exec("ls -l", function(error, stdout, stderr) { console.log(error || stdout || stderr); });
For #2, I am not sure if you could directly execute a "c" program, so I guess you'd need to do something like this
var exec = require('child_process').exec; exec("gcc ~/tools/myprog.c -o prime && ./prime", function(error, stdout, stderr) { console.log(error || stdout || stderr); });
Above code is definitely not thread safe and if concurrent requests are made, it will break, Ideally you'd not compile it for every request, you'd compile and cache for first request (you can match time stamp of source and compiled output, if compiled output is absent or its time stamp is less - earlier then the source code then you need to compile it) and then use cached output on subsequent requests.
In express you can do something like below
var exec = require('child_process').exec; router.get('/prime/:num',function(req, res, next){ exec("gcc ~/tools/myprog.c -o prime && ./prime " + req.params.num, function(error, stdout, stderr) { if(error){ res.status(400).json({'message': error.message}); return; } if(stderr){ res.status(400).json({'message': stderr}); return; } res.status(200).json({'result': stdout}); }); });
You can play with above code snippets and get what you are looking for.
Hope this helps. Please let me know if you need anything else.