-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
109 lines (97 loc) · 3.19 KB
/
server.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
const express = require("express")
const app = express()
const path = require("path")
const bodyParser = require('body-parser');
const multer = require('multer');
const fs = require('fs');
const { spawn, spawnSync } = require("child_process")
app.use(bodyParser.json())
app.use(multer().single())
app.use(express.static(path.join(__dirname, "dist", "compiler")))
var programFile;
app.post("/upload", (req, res) => {
var lang = req.body.lang;
console.log(lang);
switch (lang) {
case "c":
programFile = path.join(__dirname, "uploads", "tmp.c");
break;
case "c++":
programFile = path.join(__dirname, "uploads", "tmp.cpp");
break;
case "python":
programFile = path.join(__dirname, "uploads", "tmp.py");
break;
}
try {
if (fs.existsSync(path.join(__dirname), "uploads")) {
console.log("folder exists");
fs.chmodSync(path.join(__dirname,"uploads"),777)
} else {
fs.mkdirSync(path.join(__dirname, "uploads"))
console.log("folder created");
}
var exists = fs.existsSync(programFile);
} catch (error) {
console.log(error);
}
if (exists) {
console.log("files exists: " + programFile);
fs.writeFileSync(programFile, req.body.code, { mode: 777 })
} else {
console.log("file does not exists");
fs.writeFileSync(programFile, req.body.code, { mode: 777 })
}
var compiler = null;
var op = new Object;
if (lang == "c" ) {
mid = spawnSync('gcc', [programFile]);
if (mid.stderr.toString) {
console.log(mid.stderr.toString());
}
console.log("file written successfully");
compiler = spawn(path.join(__dirname, './a.out'));
prepareOp(compiler, res, programFile)
} else if (lang == "c++") {
var mid = spawnSync('g++', [programFile]);
console.log(mid.stdout.toString());
console.log(mid.stderr.toString());
var mid = fs.chmodSync(path.join(__dirname, "a.out"), 777)
compiler = spawn(path.join(__dirname, './a.out'));
prepareOp(compiler, res, programFile)
}
else if (lang == "python") {
compiler = spawn('python3', [programFile]);
prepareOp(compiler, res, programFile);
}
});
app.listen(3000, () => {
console.log("running on port 3000");
})
function prepareOp(compiler, res, programFile) {
var op = new Object;
compiler.stdout.on('data', (data) => {
str = String(data)
console.log(`stdout: ${str}`);
op.out = str;
});
compiler.stderr.on('data', (data) => {
str = String(data)
console.error(`stderr: ${str}`);
op.err = str;
});
compiler.on('close', (code) => {
op.end = `process exited with code ${code}`
console.log(op.end);
try {
var cleanup = fs.unlinkSync(programFile)
if (fs.existsSync(path.join(__dirname, "a.out"))) {
var cleanup = fs.unlinkSync(path.join(__dirname, "a.out"))
}
res.json(op);
} catch (error) {
console.log(error);
}
});
return op;
}