-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
121 lines (101 loc) · 3.22 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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
var child;
var format = require("util").format,
util = require("util"),
sys = require("sys"),
http = require('http'),
url = require("url"),
express = require("express"),
exec = require('child_process').exec,
spawn = require('child_process').spawn,
expose = require('express-expose'),
querystring = require("querystring"),
fs = require('fs'),
path = require('path'),
formidable = require('formidable');
var app = module.exports = express.createServer();
// Don't crash on errors.
process.on("uncaughtException", function(error) {
util.log(error.stack);
});
app.configure(function(){
app.set('views', __dirname + '/views');
app.set('view engine', 'jade');
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(require('stylus').middleware({ src: __dirname + '/public' }));
app.use(app.router);
app.use(express.static(__dirname + '/public'));
});
app.configure('development', function(){
app.use(express.errorHandler({ dumpExceptions: true, showStack: true }));
});
app.configure('production', function(){
app.use(express.errorHandler());
});
app.use(express.bodyParser());
app.use(app.router);
//Routes
function addHeaders(res) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "X-Requested-With");
}
app.all('/*',function(req, res, next) {
addHeaders(res);
next();
});
app.get('/', function(req, res){
res.render('index', { title: "Remote Run Test" } );
});
app.get('/about', function(req, res){
res.render('about', { title: 'Help' });
});
app.post('/refresh', function(req, res, next) {
refresh(req.body.data, res);
});
app.post('/start/', function(req, res, next) {
startMath(req, res);
});
app.post('/stop/', function(req, res, next) {
stopMath(req, res);
});
app.post('/upload', function(req, res, next){
//fs.renameSync(req.files.upload.path, "data\\" + req.files.upload.name);
fs.renameSync(req.files.upload.path, path.join("data", "in.txt"));
console.log("Upload OK");
res.redirect('back');
/*res.send(format('\nuploaded %s (%d Kb) to %s as %s'
, req.files.upload.name
, req.files.upload.size / 1024 | 0
, req.files.upload.path
, req.body.title));*/
});
function startMath(req, res){
if(child != undefined){
child.kill();
}
console.log("Request handler START was called.");
//var err/or="";
//var runMath = "C:\\\"Program Files\"\\Java\\jre7\\bin\\java.exe -jar test.jar";
var runMath = "java -jar test.jar";
child = exec(runMath, function (error) {
child.stderr.on('error', function (error) {
console.log(child.pid+ ' stderr: ' + error);
fs.writeFile(path.join('data','errorLog.txt'), error, function (err) {
if (err) throw err;
console.log('It\'s saved!');
});
});
var log = fs.readFileSync(path.join("data", "log.txt")).toString("utf8");
console.log(log);
if (error == undefined) {
res.send({ "log": log });
res.end();
} else {
res.send({ "error": error });
res.end();
console.log('error:' + error);
}
});
}
app.listen(80);
console.log("Express server listening on port %d in %s mode", app.address().port, app.settings.env);