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