0

Below is the code which gets the data stream from unix server:

SSH.prototype.execute = function(shellCommand, options,req, res, callback) {

  function onExec(err, stream) {
    if(err) {
      callback(err);
    } else {
      var context = {stdout: "", stderr: ""};
      stream.on('close', function(code, signal) {
        context.code = code;
        context.signal = signal;
        callback(null, context);
      }).on('data', function(data) {
        data = data.toString();
        res.write(data);//Manoj
        console.log(data);//Manoj
        context.stdout += data;
        options.onStdout(data);
      }).stderr.on('data', function(data) {
        data = data.toString();
        context.stderr += data;
        options.onStderr(data);
      });
    }
  }
 };
}

Below is the code which makes the post request to the express app:

var url = "http://localhost:5555/submit";
            $.post(url, formData).done(function (data) {
                //alert(data);
                location.reload();
                console.log(data);
            });

I am not able to use res.send() as it ends the request, thus using res.write.

I have tried using res.write with sample code through postman as below but the data is being sent at the end.

app.get('/test', function(req, res) {
  res.write('statement1');

  sleep(5000).then(() => {
    res.write('statement2');
    res.write('statement3');
    res.end();
  })

All the statments come together.

How can I send a data as a stream from node.js to a front end using jQuery to make POST request to the node.js application.

MANOJ
  • 666
  • 3
  • 8
  • 23
  • data to be streamed might takes 8 minutes to complete for each request as messages come every few seconds. Would appreciate the suggestions on how to deal this issue and other workarounds as well. – MANOJ Mar 25 '18 at 07:55
  • Possible duplicate of [Streaming data with Node.js](https://stackoverflow.com/questions/2558606/streaming-data-with-node-js) – Roamer-1888 Mar 25 '18 at 08:08

0 Answers0