// Reference: https://hub.packtpub.com/making-simple-web-based-ssh-client-using-nodejs-and-socketio/ var express = require('express'); var https = require('https'); var http = require('http'); var fs = require('fs'); var pty = require('node-pty'); // Set port var port = 9774; // Set up express server let app = express(); // Set HTTP server root folder app.use("/", express.static("./")); // Creating an HTTP server var server = http.createServer(app).listen(port); console.log(`Listening on port ${port}`); var io = require('socket.io')(server); // When a new socket connects io.on('connection', function (socket) { console.log('Client connect'); // Create terminal var term = pty.spawn('sh', [], { name: 'xterm-color', cols: 80, rows: 30, cwd: process.env.HOME, env: process.env }); // Listen on the terminal for output and send it to the client term.on('data', function (data) { socket.emit('output', data); }); // Listen on the client and send any input to the terminal socket.on('input', function (data) { term.write(data); }); // When socket disconnects, destroy the terminal socket.on("disconnect", function () { term.destroy(); console.log("Client disconnect"); }); });