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.