]> git.proxmox.com Git - mirror_xterm.js.git/blob - demo/app.js
Bind to 127.0.0.1 on Windows only
[mirror_xterm.js.git] / demo / app.js
1 var express = require('express');
2 var app = express();
3 var expressWs = require('express-ws')(app);
4 var os = require('os');
5 var pty = require('pty.js');
6
7 app.use('/src', express.static(__dirname + '/../src'));
8 app.use('/addons', express.static(__dirname + '/../addons'));
9
10 app.get('/', function(req, res){
11 res.sendFile(__dirname + '/index.html');
12 });
13
14 app.get('/style.css', function(req, res){
15 res.sendFile(__dirname + '/style.css');
16 });
17
18 app.get('/main.js', function(req, res){
19 res.sendFile(__dirname + '/main.js');
20 });
21
22 app.ws('/bash', function(ws, req) {
23 /**
24 * Open bash terminal and attach it
25 */
26 var term = pty.spawn(process.platform === 'win32' ? 'cmd.exe' : 'bash', [], {
27 name: 'xterm-color',
28 cols: 80,
29 rows: 24,
30 cwd: process.env.PWD,
31 env: process.env
32 });
33 term.on('data', function(data) {
34 try {
35 ws.send(data);
36 } catch (ex) {
37 // The WebSocket is not open, ignore
38 }
39 });
40 ws.on('message', function(msg) {
41 term.write(msg);
42 });
43 ws.on('close', function () {
44 console.log('close');
45 process.kill(term.pid);
46 });
47 });
48
49 var port = process.env.PORT || 3000,
50 host = os.platform() === 'win32' ? '127.0.0.1' : '0.0.0.0';
51
52 console.log('App listening to http://' + host + ':' + port);
53 app.listen(port, host);