Skip to content

Instantly share code, notes, and snippets.

Created April 28, 2011 22:49
Show Gist options
  • Save mixonic/947512 to your computer and use it in GitHub Desktop.
Save mixonic/947512 to your computer and use it in GitHub Desktop.
Node.js + + Bash. A collaborative terminal for your browser.
// This server will start a bash shell and expose it
// over to a browser. See ./term.html for the
// client side.
// You should probably:
// npm install
// curl -O
// To get in the node_modules directory and
// the file needed for the client.
// To start the server:
// node server.js
// And then load up your term!
// open http://`hostname`:8080/term.html
// You can even share the url with a friend on your
// local network. Be sure they're a friend though :-)
var http = require('http'),
url = require('url'),
path = require('path'),
fs = require('fs'),
io = require('./node_modules/'),
sys = require('sys'),
util = require('util'),
spawn = require('child_process').spawn;
var sh = spawn('bash');
sh.stdout.on('data', function(data) {
sh.stderr.on('data', function(data) {
sh.on('exit', function (code) {
listener.broadcast('** Shell exited: '+code+' **');
server = http.createServer(function(request, response){
var uri = url.parse(request.url).pathname;
var filename = path.join(process.cwd(), uri);
path.exists(filename, function(exists) {
if (!exists) {
response.writeHead(404, {'Content-Type':'text/plain'});
response.end("Can''t find it...");
fs.readFile(filename, 'binary',function(err, file){
if (err) {
response.writeHead(500, {'Content-Type':'text/plain'});
response.end(err + "\n");
response.write(file, 'binary');
var listener = io.listen(server);
listener.on('connection', function(client){
client.on('message', function(data){
listener.broadcast(new Buffer("> "+data));
<!doctype html>
<script src=""></script>
<script src="" type="text/javascript" charset="utf-8"></script>
<script type="text/javascript" charset="utf-8">
function add_content(str){
$('div').append('<p>' + $('<div/>').text(str).html() + '</p>');
$(window).scrollTop($('body').height() - $(window).height() + 80);
var socket = new io.Socket(null, {rememberTransport: false, port: 8080});
socket.addEvent('message', function(data) {
add_content($.map(data, function(e,i) {
return String.fromCharCode(e);
var input = $('input');
return false;
<style type="text/css">
body, p, input {
font-family: fixed;
font-size: 13px;
border: none;
p { white-space: pre; }
p, form, input { margin:0; padding:0; }
input:focus { outline: none; border: none; }
&gt; <input>
Copy link

Copy link

Anyway, a good piece of code.

Copy link

All I get when I try to run node server.js is this error message:
throw er; // Unhandled 'error' event
Error: spawn ENOENT
at errnoException (child_process.js:1011:11)
at Process.ChildProcess._handle.onexit (child_process.js:802:34)

any idea what's not working right?
(followed the instructions in the comment of server.js


Copy link

adamhipster commented Feb 15, 2017

I refactored it with Express (in a quick and dirty way) and updated the method calls of socket-io.

What's the license? Is it MIT? Then I can incorporate it as well :)

Copy link

Thanks! @melvinroest

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment