-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApi.js
69 lines (63 loc) · 2.18 KB
/
Api.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
const express = require('express');
const app = express();
const bodyParser = require('body-parser');
const compiler = require('./node_modules/compilex'); // Ensure this path is correct
const options = { stats: true };
compiler.init(options);
app.use(bodyParser.json());
app.use(express.static(__dirname)); // To serve static files like index.html and codemirror
app.get('/', (req, res) => {
compiler.flush(() => {
console.log("Deleted temporary files.");
});
res.sendFile(__dirname + "/index.html");
});
app.post('/compile', (req, res) => {
const { code, input, lang } = req.body;
const compileCallback = (data) => {
if (data.error) {
console.error(`Compilation error for ${lang}: ${data.error}`);
res.send({ output: data.error });
} else {
res.send({ output: data.output });
}
};
const envData = { OS: "windows" }; // Adjust based on your environment
switch (lang) {
case "C":
envData.cmd = "gcc";
if (input) {
compiler.compileCPPWithInput(envData, code, input, compileCallback);
} else {
compiler.compileCPP(envData, code, compileCallback);
}
break;
case "C++":
envData.cmd = "g++";
if (input) {
compiler.compileCPPWithInput(envData, code, input, compileCallback);
} else {
compiler.compileCPP(envData, code, compileCallback);
}
break;
case "Java":
if (input) {
compiler.compileJavaWithInput(envData, code, input, compileCallback);
} else {
compiler.compileJava(envData, code, compileCallback);
}
break;
case "Python":
if (input) {
compiler.compilePythonWithInput(envData, code, input, compileCallback);
} else {
compiler.compilePython(envData, code, compileCallback);
}
break;
default:
res.send({ output: "Unsupported language" });
}
});
app.listen(8000, () => {
console.log('Server is running on port 8000');
});