title: IPC : Node JS server communication with simple C code
body: <p>I have a C code, that within a infinite loop waits for an input and produces an output.</p>
  
  <pre><code>#include&lt;stdio.h&gt;
  void flush() {
      int c;
      while ((c = getchar()) != '\n' &amp;&amp; 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();
  
      }
  }
  </code></pre>
  
  <p>I need another Node JS service that will listen to on some port and send the output as response. 
  I have this code written</p>
  
  <pre><code>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);
  </code></pre>
  
  <p>This code I have doesn't seem seem working at all.</p>
  
  <p>Node JS server terminates with an error and the web response consist of 283 lines of response instead of 1.</p>
  
  <p>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.</p>