-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
61 lines (52 loc) · 1.39 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
var fs = require('fs'),
path = require('path'),
express = require('express'),
bodyParser = require('body-parser'),
app = express(),
assert = require('assert'),
sys = require('sys'),
exec = require('child_process').exec;
app.use('/', express.static(path.join(__dirname, 'public')));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: true}));
/**
Correspond to the 'Submit' post event from client.
Run the program uploaded from client.
*/
app.post('/runProgram', function(req, res) {
var language = req.body.language,
runProgramOutput,
cmd;
function puts(error, stdout, stderr) {
runProgramOutput = stdout;
if (stderr.length > 0) {
runProgramOutput = stderr;
}
res.setHeader('Content-Type', 'application/json');
res.send(JSON.stringify(runProgramOutput.toString()));
}
// Saving uploaded script in a local tmp file
fs.writeFile('./tmp', req.body.content, function(err){
});
/**
@language: the langauge chosen by the client.
options: python, javascript, c_cpp
*/
switch (language) {
case 'python':
cmd = 'python';
exec(cmd + " tmp", puts);
break;
case 'javascript':
cmd = 'node';
exec(cmd + " tmp", puts);
break;
case 'c_cpp':
cmd = 'g++';
fs.rename('./tmp', './tmp.cpp');
exec(cmd + " -o a.out tmp.cpp; ./a.out", puts);
break;
}
});
app.listen(3001);
console.log('Server started: http://localhost:3001/');