Skip to content

Instantly share code, notes, and snippets.

@serverwentdown
Forked from mixonic/server.js
Last active January 29, 2019 20:18
Show Gist options
  • Save serverwentdown/8176715 to your computer and use it in GitHub Desktop.
Save serverwentdown/8176715 to your computer and use it in GitHub Desktop.
// Bug fixes for current versions.
//
// This server will start a bash shell and expose it
// over socket.io to a browser. See ./term.html for the
// client side.
//
// You should probably:
//
// npm install socket.io
// curl -O https://github.com/LearnBoost/Socket.IO/raw/master/socket.io.min.js
//
// To get socket.io in the node_modules directory and
// the socket.io.min.js 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('socket.io'),
sys = require('sys'),
util = require('util'),
spawn = require('child_process').spawn;
server = http.createServer(function(request, response){
var uri = url.parse(request.url).pathname;
var filename = path.join(process.cwd(), uri);
fs.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");
return;
}
response.writeHead(200);
response.write(file, 'binary');
response.end();
});
});
}
);
server.listen(8080);
var listener = io.listen(server);
listener.on('connection', function(client){
var sh = spawn('bash');
client.on('message', function(data){
sh.stdin.write(data+"\n");
client.send(new Buffer("> "+data));
});
sh.stdout.on('data', function(data) {
client.send(data);
});
sh.stderr.on('data', function(data) {
client.send(data);
});
sh.on('exit', function (code) {
client.send('** Shell exited: '+code+' **');
});
});
<!doctype html>
<html>
<head>
<script src="http://code.jquery.com/jquery-latest.min.js"></script>
<script src="socket.io/socket.io.js" type="text/javascript" charset="utf-8"></script>
<script type="text/javascript" charset="utf-8">
function add_content(str){
console.log(str);
$('div').append('<p>' + $('<div/>').text(str).html() + '</p>');
$(window).scrollTop($('body').height() - $(window).height() + 80);
}
var socket = io.connect();
socket.on('message', function(data) {
add_content(data);
});
$(function(){
$('form').submit(function(e){
var input = $('input');
socket.send(input.val());
input.val('');
return false;
});
$('input').focus();
});
</script>
<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; }
</style>
</head>
<body>
<div></div>
<form>
&gt; <input>
</form>
</body>
</html>
@thatkookooguy
Copy link

All I get when I try to run node server.js is this error message:
events.js:72
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

thanks!

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