5

I am attempting to pipe the stdout & stdin of a child_process to a browser & display it in an html page. I am using browserify to get node.js to run on the browser. My code for spawning the child_process is like this.

var child = require('child_process');

var myREPL = child.spawn('myshell.exe', ['args']);

 // myREPL.stdout.pipe(process.stdout, { end: false });

 process.stdin.resume();

 process.stdin.pipe(myREPL.stdin, { end: false });

 myREPL.stdin.on('end', function() {
   process.stdout.write('REPL stream ended.');
 });

 myREPL.on('exit', function (code) {
   process.exit(code);
 });

 myREPL.stdout.on('data', function(data) {
    console.log('\n\nSTDOUT: \n');
    console.log('**************************');
    console.log('' + data);
    console.log('==========================');
 });

I created a bundle.js using browserify and my html looks like this.

<!doctype html>
    <html lang="en">
        <head>
            <meta charset="utf-8" />
            <title></title>
            <!--[if IE]>
            <script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script>
            <![endif]-->
            <script src="bundle.js"></script>
            <script src="main.js"></script>
        </head>
        <body>

        </body>
    </html>

I am trying to avoid running an http server and piping the results to it in the browser. Is there any other way where I can do it ? Thanks

ssarangi
  • 602
  • 1
  • 10
  • 28
  • What's the problem? Any error messages? – Jeremiah Orr Jun 26 '13 at 00:41
  • yeah, so in the browser process.stdin & process.stdout are undefined which sort of makes sense since the browser is not expected to support it. But I am not sure how to work around it – ssarangi Jun 26 '13 at 00:57

3 Answers3

4

You should look into hyperwatch, which pipes the server side stdout/stderr to the browser and renders it exactly like it would appear in your terminal (including colors).

If it doesn't exactly solve your problem, reading through the code should at least help you. It uses hypernal under the hood in order to convert terminal output to html.

Thorsten Lorenz
  • 11,781
  • 8
  • 52
  • 62
  • Very nice stuff, with a complete example and open license :) thank you – Andrei Feb 25 '15 at 12:29
  • I think the frontail NPM module can also do the same thing - https://github.com/mthenw/frontail, I have used it and it works – Alexander Mills Mar 30 '17 at 21:01
  • `hypernal` works great! This is the only library I've found that is purely for rendering output (not simulating an actual terminal) and does a good job supporting escape codes like colors, as well as control codes, like cursor moves. Most solutions fail badly on that point. – Aaron Beall Oct 23 '19 at 17:11
1

I dont know if this is to late but i managed to run a program from browser starting from this code that works only on linux (i use ubuntu). You will have to run interactive programs with stdbuf -o0 prefix.

var child = require('child_process');
var myREPL = child.spawn('bash');

process.stdin.pipe(myREPL.stdin);

myREPL.stdin.on("end", function() {
    process.exit(0);
});

myREPL.stdout.on('data', function (data) {
    console.log(data+'');
});

myREPL.stderr.on('data', function (data) {
    console.log('stderr: ' + data);
});

An then to make it to work on browser you only need to add socket.io

var myREPL = child.spawn(program);
    myREPL.stdin.on("end", function() {
        socket.emit('consoleProgramEnded');
    });

    myREPL.stdout.on('data', function (data) {
        socket.emit('consoleWrite',data+'');
    });

    myREPL.stderr.on('data', function (data) {
        socket.emit('consoleWrite',data+'');
    });

    socket.on('consoleRead',function(message){
        console.log("Writing to console:"+message);
        myREPL.stdin.write(message.replace("<br>","")+"\n");
    });

I hope that will help you.

alexand7u
  • 11
  • 3
0

I think the frontail NPM module can also do what you want to do -

https://github.com/mthenw/frontail

I have used it and it works

Alexander Mills
  • 90,741
  • 139
  • 482
  • 817