Skip to content

Instantly share code, notes, and snippets.

@luggage66
Created December 22, 2016 00:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save luggage66/261c2f4f164f4357a898234a17ae36e7 to your computer and use it in GitHub Desktop.
Save luggage66/261c2f4f164f4357a898234a17ae36e7 to your computer and use it in GitHub Desktop.
Example: running a process and piping output back to web client
import { spawn } from 'child_process';
// app is an express server
app.get('/some/url/:someOption' function(req, res, next) {
let { someOption } = req.params;
let commandProcess = spawn('node', ['someScript.js', someOption]);
commandProcess.stdout.setEncoding('utf8');
commandProcess.stderr.pipe(process.stderr);
commandProcess.stdin.write("something on std in");
commandProcess.stdin.end();
commandProcess.stdout.pipe(res);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment