Spaces:
Sleeping
Sleeping
Update index.js
Browse files
index.js
CHANGED
|
@@ -16,40 +16,73 @@ app.get("/", function (req, res) {
|
|
| 16 |
res.send("Hello World!!");
|
| 17 |
});
|
| 18 |
|
| 19 |
-
app.post("/compile", (req, res) => {
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
// Write the Java code to Main.java file
|
| 23 |
-
fs.writeFile("/code/temp/Main.java", code, (err) => {
|
| 24 |
-
if (err) {
|
| 25 |
-
console.error(err);
|
| 26 |
-
res.status(500).send("Error writing Java file");
|
| 27 |
-
return;
|
| 28 |
-
}
|
| 29 |
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
return;
|
| 38 |
-
}
|
| 39 |
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 50 |
}
|
| 51 |
});
|
| 52 |
-
});
|
| 53 |
|
| 54 |
const port = 7860;
|
| 55 |
app.listen(port, () => {
|
|
|
|
| 16 |
res.send("Hello World!!");
|
| 17 |
});
|
| 18 |
|
| 19 |
+
// app.post("/compile", (req, res) => {
|
| 20 |
+
// const { code, input } = req.body; // Assuming code and input are sent as POST parameters
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
|
| 22 |
+
// // Write the Java code to Main.java file
|
| 23 |
+
// fs.writeFile("/code/temp/Main.java", code, (err) => {
|
| 24 |
+
// if (err) {
|
| 25 |
+
// console.error(err);
|
| 26 |
+
// res.status(500).send("Error writing Java file");
|
| 27 |
+
// return;
|
| 28 |
+
// }
|
|
|
|
|
|
|
| 29 |
|
| 30 |
+
// // Compile and run the Java code with input
|
| 31 |
+
// const javaProcess = exec(
|
| 32 |
+
// "javac -d /code/temp /code/temp/Main.java && java -cp /code/temp Main",
|
| 33 |
+
// (error, stdout, stderr) => {
|
| 34 |
+
// if (error) {
|
| 35 |
+
// console.error(error);
|
| 36 |
+
// res.send({error:stderr});
|
| 37 |
+
// return;
|
| 38 |
+
// }
|
| 39 |
+
|
| 40 |
+
// console.log(stdout);
|
| 41 |
+
// console.error(stderr);
|
| 42 |
+
// res.send({output:stdout});
|
| 43 |
+
// }
|
| 44 |
+
// );
|
| 45 |
|
| 46 |
+
// // Pass input to the Java process
|
| 47 |
+
// if (input) {
|
| 48 |
+
// javaProcess.stdin.write(input);
|
| 49 |
+
// javaProcess.stdin.end();
|
| 50 |
+
// }
|
| 51 |
+
// });
|
| 52 |
+
// });
|
| 53 |
+
|
| 54 |
+
app.post("/compile", function (req, res) {
|
| 55 |
+
var code = req.body.code;
|
| 56 |
+
var input = req.body.input;
|
| 57 |
+
var lang = req.body.lang;
|
| 58 |
+
console.log(code + " " + input + " " + lang);
|
| 59 |
+
|
| 60 |
+
try {
|
| 61 |
+
if (lang == "Python") {
|
| 62 |
+
if (!input) {
|
| 63 |
+
var envData = { OS: "linux" };
|
| 64 |
+
compiler.compilePython(envData, code, function (data) {
|
| 65 |
+
if (data.output) {
|
| 66 |
+
res.send(data);
|
| 67 |
+
} else {
|
| 68 |
+
res.send({ error: data.error });
|
| 69 |
+
}
|
| 70 |
+
});
|
| 71 |
+
} else {
|
| 72 |
+
var envData = { OS: "linux" };
|
| 73 |
+
compiler.compilePythonWithInput(envData, code, input, function (data) {
|
| 74 |
+
if (data.output) {
|
| 75 |
+
res.send(data);
|
| 76 |
+
} else {
|
| 77 |
+
res.send({ error: data.error });
|
| 78 |
+
}
|
| 79 |
+
});
|
| 80 |
+
}
|
| 81 |
+
}
|
| 82 |
+
} catch (e) {
|
| 83 |
+
console.log("error:" + e);
|
| 84 |
}
|
| 85 |
});
|
|
|
|
| 86 |
|
| 87 |
const port = 7860;
|
| 88 |
app.listen(port, () => {
|