Spaces:
Sleeping
Sleeping
File size: 3,132 Bytes
a55949b 314a3e4 a55949b 314a3e4 a55949b 336b464 a55949b 00320d3 6eb59c6 84c00ca 00320d3 84c00ca 00320d3 84c00ca 00320d3 84c00ca 00320d3 84c00ca 69ca45c f311e4e 69ca45c bc37f26 69ca45c 218a33b 69ca45c f311e4e 69ca45c bc37f26 69ca45c f311e4e 69ca45c 314a3e4 6eb59c6 e7e6e29 314a3e4 69ca45c |
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 |
const express = require("express");
const app = express();
const bodyParser = require("body-parser");
const cors = require("cors");
const fs = require("fs");
const { exec } = require("child_process");
const compiler = require("compilex");
const options = { stats: true };
compiler.init(options);
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
app.use(cors());
app.get("/", function (req, res) {
res.send("Hello World!!");
});
const TIMEOUT_VALUE = 10000;
app.post("/compile", function (req, res) {
var code = req.body.code;
var input = req.body.input;
var lang = req.body.lang;
try {
if (lang == "Python") {
if (!input) {
var envData = { OS: "linux" };
compiler.compilePython(envData, code, function (data) {
if (data.output) {
res.send(data);
} else {
res.send({ error: data.error });
}
});
} else {
var envData = { OS: "linux" };
compiler.compilePythonWithInput(envData, code, input, function (data) {
if (data.output) {
res.send(data);
} else {
res.send({ error: data.error });
}
});
}
}else if(lang == "Java"){
fs.writeFile("/code/temp/Main.java", code, (err) => {
if (err) {
console.error(err);
res.status(500).send("Error writing Java file");
return;
}
const javaProcess = exec(
"javac -d /code/temp /code/temp/Main.java && java -cp /code/temp Main",
{ timeout: TIMEOUT_VALUE }, // Add timeout option
(error, stdout, stderr) => {
if (error) {
console.error(error);
res.send({ error: error.message });
return;
}
console.error(stderr);
res.send({output:stdout});
}
);
if (input) {
javaProcess.stdin.write(input);
javaProcess.stdin.end();
}
});
}
else if (lang == "Cpp") {
fs.writeFile("/code/temp/Main.cpp", code, (err) => {
if (err) {
console.error(err);
res.status(500).send("Error writing C++ file");
return;
}
const cppProcess = exec(
"g++ -o /code/temp/Main /code/temp/Main.cpp && /code/temp/Main",
{ timeout: TIMEOUT_VALUE }, // Add timeout option
(error, stdout, stderr) => {
if (error) {
console.error(error);
res.send({ error: error.message });
return;
}
console.error(stderr);
res.send({ output: stdout });
}
);
if (input) {
cppProcess.stdin.write(input);
cppProcess.stdin.end();
}
});
}
} catch (e) {
console.log("error:" + e);
}
});
const port = 7860;
app.listen(port, () => {
console.log(`Open http://localhost:${port}`);
}); |