I have a C code, that within a infinite loop waits for an input and produces an output.
#include<stdio.h>
void flush() {
int c;
while ((c = getchar()) != '\n' && c != EOF);
}
int main() {
/*
* Load many files here
*/
double d = 0;
char input[1024];
while (1) {
d = d + 0.1;
scanf("%[^\n]", input);
printf("%lf\n",d);
fflush(stdout);
flush();
}
}
I need another Node JS service that will listen to on some port and send the output as response. I have this code written
var http = require('http');
var spawn = require('child_process').spawn;
var child = spawn('./dummyNLU.out');
child.stdin.setEncoding('utf-8');
child.stdin.cork();
var buffer = new Buffer(100);
var app = http.createServer(function(req,res){
var string = buffer.write("HelloWorld!"); //this is for testing purpose
child.stdin.write(buffer);
child.stdin.end();
child.stdout.on('data', function(data) {
res.setHeader('Content-Type', 'text/plain');
res.end(data);
});
});
app.listen(3001);
This code I have doesn't seem seem working at all.
Node JS server terminates with an error and the web response consist of 283 lines of response instead of 1.
Can anyone please help me?? Some other approaches to solve the problem (reply to web request from a C executable code output) are also welcome. Thank you in advance.