Spaces:
Sleeping
Sleeping
Update index.js
Browse files
index.js
CHANGED
@@ -44,85 +44,64 @@ app.post("/compile", function (req, res) {
|
|
44 |
}
|
45 |
});
|
46 |
}
|
47 |
-
}
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
console.log("error:" + e);
|
54 |
-
}
|
55 |
-
});
|
56 |
-
|
57 |
-
const compileJava = (code, input, res) => {
|
58 |
-
fs.writeFile("/code/temp/Main.java", code, (err) => {
|
59 |
-
if (err) {
|
60 |
-
console.error(err);
|
61 |
-
res.status(500).send("Error writing Java file");
|
62 |
-
return;
|
63 |
-
}
|
64 |
-
const javaProcess = exec(
|
65 |
-
"javac -d /code/temp /code/temp/Main.java && java -cp /code/temp Main",
|
66 |
-
{ timeout: TIMEOUT_VALUE },
|
67 |
-
(error, stdout, stderr) => {
|
68 |
-
if (error) {
|
69 |
-
if (error.code === 1) {
|
70 |
-
console.error(stderr);
|
71 |
-
res.send({ error: stderr });
|
72 |
-
} else if (error.killed) {
|
73 |
-
console.error("Time Limit Exceeded!!");
|
74 |
-
res.send({ error: "Time Limit Exceeded!!" });
|
75 |
-
} else {
|
76 |
-
console.error(stderr);
|
77 |
-
res.send({ error: stderr });
|
78 |
}
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
83 |
}
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
};
|
91 |
-
|
92 |
-
const compileCpp = (code, input, res) => {
|
93 |
-
fs.writeFile("/code/temp/Main.cpp", code, (err) => {
|
94 |
-
if (err) {
|
95 |
-
console.error(err);
|
96 |
-
res.status(500).send("Error writing C++ file");
|
97 |
-
return;
|
98 |
-
}
|
99 |
-
const cppProcess = exec(
|
100 |
-
"g++ -o /code/temp/Main /code/temp/Main.cpp && /code/temp/Main",
|
101 |
-
{ timeout: TIMEOUT_VALUE }, // Add timeout option
|
102 |
-
(error, stdout, stderr) => {
|
103 |
-
if (error) {
|
104 |
-
if (error.killed) {
|
105 |
-
console.error("Time Limit Exceeded!!");
|
106 |
-
res.send({ error: "Time Limit Exceeded!!" });
|
107 |
-
} else {
|
108 |
-
console.error(stderr);
|
109 |
-
res.send({ error: stderr });
|
110 |
}
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
115 |
}
|
116 |
-
)
|
117 |
-
|
118 |
-
cppProcess.stdin.write(input);
|
119 |
-
cppProcess.stdin.end();
|
120 |
}
|
121 |
});
|
122 |
-
};
|
123 |
-
|
124 |
|
125 |
const port = 7860;
|
126 |
app.listen(port, () => {
|
127 |
console.log(`Open http://localhost:${port}`);
|
128 |
-
});
|
|
|
44 |
}
|
45 |
});
|
46 |
}
|
47 |
+
}else if(lang == "Java"){
|
48 |
+
fs.writeFile("/code/temp/Main.java", code, (err) => {
|
49 |
+
if (err) {
|
50 |
+
console.error(err);
|
51 |
+
res.status(500).send("Error writing Java file");
|
52 |
+
return;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
53 |
}
|
54 |
+
const javaProcess = exec(
|
55 |
+
"javac -d /code/temp /code/temp/Main.java && java -cp /code/temp Main",
|
56 |
+
{ timeout: TIMEOUT_VALUE }, // Add timeout option
|
57 |
+
(error, stdout, stderr) => {
|
58 |
+
if (error) {
|
59 |
+
console.error(error);
|
60 |
+
res.send({error: "Time Limit Exceeded!!"});
|
61 |
+
return;
|
62 |
+
}
|
63 |
+
console.error(stderr);
|
64 |
+
res.send({output:stdout});
|
65 |
+
}
|
66 |
+
);
|
67 |
+
if (input) {
|
68 |
+
javaProcess.stdin.write(input);
|
69 |
+
javaProcess.stdin.end();
|
70 |
+
}
|
71 |
+
});
|
72 |
}
|
73 |
+
else if (lang == "Cpp") {
|
74 |
+
fs.writeFile("/code/temp/Main.cpp", code, (err) => {
|
75 |
+
if (err) {
|
76 |
+
console.error(err);
|
77 |
+
res.status(500).send("Error writing C++ file");
|
78 |
+
return;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
79 |
}
|
80 |
+
const cppProcess = exec(
|
81 |
+
"g++ -o /code/temp/Main /code/temp/Main.cpp && /code/temp/Main",
|
82 |
+
{ timeout: TIMEOUT_VALUE }, // Add timeout option
|
83 |
+
(error, stdout, stderr) => {
|
84 |
+
if (error) {
|
85 |
+
console.error(error);
|
86 |
+
res.send({ error: "Time Limit Exceeded!!" });
|
87 |
+
return;
|
88 |
+
}
|
89 |
+
console.error(stderr);
|
90 |
+
res.send({ output: stdout });
|
91 |
+
}
|
92 |
+
);
|
93 |
+
if (input) {
|
94 |
+
cppProcess.stdin.write(input);
|
95 |
+
cppProcess.stdin.end();
|
96 |
+
}
|
97 |
+
});
|
98 |
}
|
99 |
+
} catch (e) {
|
100 |
+
console.log("error:" + e);
|
|
|
|
|
101 |
}
|
102 |
});
|
|
|
|
|
103 |
|
104 |
const port = 7860;
|
105 |
app.listen(port, () => {
|
106 |
console.log(`Open http://localhost:${port}`);
|
107 |
+
});
|