node.js - NodeJS/Express - live stream stdout of child_process.spawn to client -
i have small node application using express displays form, gets values , files, , feeds elf executable. i've been able capture stdout using child_process.spawn, can't send live client.
the stdout shows on client, when res.end() called. there way show output on client produced algorithm?
here's server code (the form basic form, , stdout should render blank webpage):
// builtin libraries var spawn = require('child_process').spawn; var exec = require('child_process').exec; // external libraries var express = require('express'); var multer = require('multer'); var bodyparser = require('body-parser'); var s = require('string'); // create upload directory if doesn't exist var dir_upload = "/tmp/algorithm/files/" exec("mkdir -p " + dir_upload, {}, function (err, stdout, stderr) { if (err) throw err; }); app.use(express.static('public')); // parse text fields app.use(bodyparser.urlencoded({ extended: false })); // parse files app.use(multer({ dest : dir_upload }).fields([{name: 'out'}, {name: 'label'}, {name: 'minor'}])); // display form app.get('/', function (req, res) { res.sendfile(__dirname + '/form.html'); }) app.post('/', function (req, res) { res.writehead(200, {'content-type': 'text/plain', 'transfer-encoding': 'chunked'}); // add parameters array var args = []; var vopt = false; var vstr = ""; var keys = object.keys(req.body), len = keys.length, = 0; while (i < len) { // basic argument parsing - not shown here } // input file paths args.push(req.files.out[0].path); args.push(req.files.label[0].path); if (req.files.minor) args.push(req.files.minor[0].path); // run algorithm var command = __dirname + "/../algorithm_dir/src/algorithm"; // provide env aws centos environments old gcc versions var algorithm = spawn(command, args, { shell: true, env: { 'ld_library_path': '/usr/local/lib:/usr/lib:/usr/local/lib64:/usr/lib64' }}); var output = ""; algorithm.stdout.on('data', function (data) { res.write(data.tostring()); }); algorithm.on('close', function () { res.end("\n----------done----------"); // remove input files exec("rm -rf " + req.files.out[0].path + " " + req.files.label[0].path + " " + req.files.minor[0].path, {}, function (err, stdout, stderr) { if (err) throw err; }); }); }) // listen on port 8080 var server = app.listen(8080, function () { var host = server.address().address var port = server.address().port })
just try set header not write then, as:
res.setheader("content-type", "some/type"); then write data & close did
Comments
Post a Comment