hprasath commited on
Commit
218a33b
·
verified ·
1 Parent(s): 00320d3

Update index.js

Browse files
Files changed (1) hide show
  1. index.js +66 -48
index.js CHANGED
@@ -45,61 +45,79 @@ app.post("/compile", function (req, res) {
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, () => {
 
45
  });
46
  }
47
  }else if(lang == "Java"){
48
+ compileJava(code, input, res);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
49
  }
50
  else if (lang == "Cpp") {
51
+ compileCpp(code, input, res);
52
+ } catch (e) {
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 }, // Add timeout option
67
+ (error, stdout, stderr) => {
68
+ if (error) {
69
+ if (error.killed) {
70
+ console.error("Time Limit Exceeded!!");
71
+ res.send({ error: "Time Limit Exceeded!!" });
72
+ } else {
73
+ console.error(stderr);
74
+ res.send({ error: stderr });
75
  }
76
+ return;
77
+ }
78
+ console.log(stdout);
79
+ res.send({ output: stdout });
80
+ }
81
+ );
82
+ if (input) {
83
+ javaProcess.stdin.write(input);
84
+ javaProcess.stdin.end();
85
+ }
86
+ });
87
+ };
88
+
89
+ const compileCpp = (code, input, res) => {
90
+ fs.writeFile("/code/temp/Main.cpp", code, (err) => {
91
+ if (err) {
92
+ console.error(err);
93
+ res.status(500).send("Error writing C++ file");
94
+ return;
95
+ }
96
+ const cppProcess = exec(
97
+ "g++ -o /code/temp/Main /code/temp/Main.cpp && /code/temp/Main",
98
+ { timeout: TIMEOUT_VALUE }, // Add timeout option
99
+ (error, stdout, stderr) => {
100
+ if (error) {
101
+ if (error.killed) {
102
+ console.error("Time Limit Exceeded!!");
103
+ res.send({ error: "Time Limit Exceeded!!" });
104
+ } else {
105
+ console.error(stderr);
106
+ res.send({ error: stderr });
107
  }
108
+ return;
109
+ }
110
+ console.log(stdout);
111
+ res.send({ output: stdout });
112
  }
113
+ );
114
+ if (input) {
115
+ cppProcess.stdin.write(input);
116
+ cppProcess.stdin.end();
117
  }
118
  });
119
+ };
120
+
121
 
122
  const port = 7860;
123
  app.listen(port, () => {