Spaces:
Sleeping
Sleeping
Update index.js
Browse files
index.js
CHANGED
|
@@ -1,9 +1,9 @@
|
|
| 1 |
const express = require("express");
|
| 2 |
const app = express();
|
| 3 |
-
const
|
| 4 |
const cors = require("cors");
|
| 5 |
-
const fs = require(
|
| 6 |
-
const { exec } = require(
|
| 7 |
const compiler = require("compilex");
|
| 8 |
const options = { stats: true };
|
| 9 |
compiler.init(options);
|
|
@@ -13,116 +13,45 @@ app.use(express.urlencoded({ extended: true }));
|
|
| 13 |
app.use(cors());
|
| 14 |
|
| 15 |
app.get("/", function (req, res) {
|
| 16 |
-
|
| 17 |
res.send("Hello World!!");
|
| 18 |
});
|
| 19 |
|
| 20 |
-
app.post(
|
| 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 |
-
// console.log(code + " " + input + " " + lang);
|
| 51 |
-
|
| 52 |
-
// try {
|
| 53 |
-
// if (lang == "Cpp") {
|
| 54 |
-
// if (!input) {
|
| 55 |
-
// var envData = {
|
| 56 |
-
// OS: "linux",
|
| 57 |
-
// cmd: "g++",
|
| 58 |
-
// options: { timeout: 10000 },
|
| 59 |
-
// };
|
| 60 |
-
// compiler.compileCPP(envData, code, function (data) {
|
| 61 |
-
// if (data.output) {
|
| 62 |
-
// res.send(data);
|
| 63 |
-
// } else {
|
| 64 |
-
// res.send({ error: data.error });
|
| 65 |
-
// }
|
| 66 |
-
// });
|
| 67 |
-
// } else {
|
| 68 |
-
// var envData = {
|
| 69 |
-
// OS: "linux",
|
| 70 |
-
// cmd: "g++",
|
| 71 |
-
// options: { timeout: 10000 },
|
| 72 |
-
// };
|
| 73 |
-
// compiler.compileCPPWithInput(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 |
-
// } else if (lang == "Java") {
|
| 82 |
-
// if (!input) {
|
| 83 |
-
// var envData = { OS: "linux" };
|
| 84 |
-
// compiler.compileJava(envData, code, function (data) {
|
| 85 |
-
// if (data.output) {
|
| 86 |
-
// res.send(data);
|
| 87 |
-
// } else {
|
| 88 |
-
// res.send({ error: data.error });
|
| 89 |
-
// }
|
| 90 |
-
// });
|
| 91 |
-
// } else {
|
| 92 |
-
// var envData = { OS: "linux" };
|
| 93 |
-
|
| 94 |
-
// compiler.compileJavaWithInput(envData, code, input, function (data) {
|
| 95 |
-
// if (data.output) {
|
| 96 |
-
// res.send(data);
|
| 97 |
-
// } else {
|
| 98 |
-
// res.send({ error: data.error });
|
| 99 |
-
// }
|
| 100 |
-
// });
|
| 101 |
-
// }
|
| 102 |
-
// }else if (lang == "Python") {
|
| 103 |
-
// if (!input) {
|
| 104 |
-
// var envData = { OS: "linux" };
|
| 105 |
-
// compiler.compilePython(envData, code, function (data) {
|
| 106 |
-
// if (data.output) {
|
| 107 |
-
// res.send(data);
|
| 108 |
-
// } else {
|
| 109 |
-
// res.send({ error: data.error });
|
| 110 |
-
// }
|
| 111 |
-
// });
|
| 112 |
-
// } else {
|
| 113 |
-
// var envData = { OS: "linux" };
|
| 114 |
-
// compiler.compilePythonWithInput(envData, code, input, function (data) {
|
| 115 |
-
// if (data.output) {
|
| 116 |
-
// res.send(data);
|
| 117 |
-
// } else {
|
| 118 |
-
// res.send({ error: data.error });
|
| 119 |
-
// }
|
| 120 |
-
// });
|
| 121 |
-
// }
|
| 122 |
-
// }
|
| 123 |
-
// } catch (e) {
|
| 124 |
-
// console.log("error:" + e);
|
| 125 |
-
// }
|
| 126 |
-
// });
|
| 127 |
-
const port = 7860
|
| 128 |
-
app.listen(port, () => { console.log(`Open http://localhost:${port}`) })
|
|
|
|
| 1 |
const express = require("express");
|
| 2 |
const app = express();
|
| 3 |
+
const bodyParser = require("body-parser");
|
| 4 |
const cors = require("cors");
|
| 5 |
+
const fs = require("fs");
|
| 6 |
+
const { exec } = require("child_process");
|
| 7 |
const compiler = require("compilex");
|
| 8 |
const options = { stats: true };
|
| 9 |
compiler.init(options);
|
|
|
|
| 13 |
app.use(cors());
|
| 14 |
|
| 15 |
app.get("/", function (req, res) {
|
|
|
|
| 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.status(500).send("Error compiling or executing Java code");
|
| 37 |
+
return;
|
| 38 |
}
|
| 39 |
|
| 40 |
+
console.log(stdout);
|
| 41 |
+
console.error(stderr);
|
| 42 |
+
res.send(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 |
+
const port = 7860;
|
| 55 |
+
app.listen(port, () => {
|
| 56 |
+
console.log(`Open http://localhost:${port}`);
|
| 57 |
+
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|