What I am trying to do is set up a stream from a scala file that is writing to stdout and pipe that output to node.js. e.g. writing.scala | node index.js Then, in node.js, I want to read each line as it comes in and send it to the client. Does anyone know how I could go about doing this? Is there a better way to accomplish the same thing?
The easiest way is to call your scala program from within your node app using child_process.exec function:
var express = require('express');
var exec = require('child_process').exec;
var app = express();
app.get('/', function(req, res){
exec('your scala program',
function (error, stdout, stderr) {
res.send(stdout);
});
});
app.listen(3000);
If you don't care about errors, you can use piping to speed-up your app. In this case node will start sending data to the client immediately after calling your scala program. I'm not absolutely sure about syntax, but is should look something like this:
var express = require('express');
var exec = require('child_process').exec;
var app = express();
app.get('/', function(req, res){
var child = exec('your scala program');
child.stdout.pipe(res);
});
app.listen(3000);
Leonids response is 98% correct accept that since res is a writable stream and stdout is a readable stream the code should be be written like this:
...
exec('your scala program',
function (error, stdout, stderr) {
stdout.pipe(res);
});
...
I think here lies your answer.