-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
75 lines (62 loc) · 1.53 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
var express = require('express');
var app = express();
var http = require('http').Server(app);
var io = require('socket.io')(http);
io.on('connection', function(socket) {
console.log('socket connected, OK.');
});
//
// utility functions
//
var bgnTime;
var retCount;
var sendResponse = function(res, api, data) {
var resObj = { api: api, ret: data };
res.end();
io.emit('api-fired', resObj);
console.log(resObj);
}
var exec = require('child_process').exec;
var execCmd = function(cmd, callback) {
exec(cmd, function(error, stdout, stderr) {
console.log(stdout);
var result = { stdout: stdout };
if (error) {
console.log('error= ' + error);
result.error = error.toString();
}
callback(result);
});
}
//
// API handlers
//
app.get('/exec', function(req, res) {
bgnTime = process.hrtime();
retCount = 0;
execCmd(req.query.cmd, function(result) {
sendResponse(res, 'exec', result);
})
});
app.get('/done', function(req, res) {
endTime = process.hrtime();
if (!bgnTime) {
var endTime = process.hrtime();
sendResponse(res, 'call EXEC first');
} else {
retCount++;
var endTime = process.hrtime();
var diff = process.hrtime(bgnTime);
var diffTime = (diff[0] + diff[1]*1e-9).toFixed(2);
sendResponse(res, 'done', { count: retCount, diff: diffTime });
}
});
var path = require('path');
app.use(express.static(path.join(__dirname, 'public')));
//
// start server
//
var port = 8001;
var server = http.listen(port, function() {
console.log('listening on *:' + port);
});