-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
145 lines (117 loc) · 4.15 KB
/
app.js
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
const express = require("express");
const bodyParser = require("body-parser");
const fs = require("fs");
const childProcess = require("child_process");
const app = express();
app.set('view engine','ejs');
app.use(bodyParser.urlencoded({extended : true}));
app.use(express.static('public'));
var save_name=new Array();
var save_data=new Array();
var save_count=0;
var file_number;
var file_name;
/*****************************get**************************/
app.get("/",function(req,res){
res.render("index",{code: "", compiled: 0, output:"",fileNo: -1,file_name:""});
});
app.get('/about',function(req,res){
res.render("about");
});
app.get('/saved',function(req,res){
res.render("saved",{count: save_count,save_name: save_name});
}
);
app.get('/saved/:n',function(req,res){
if(req.params.n<save_count && req.params.n>=0){
res.render("saved_files",{name: save_name[req.params.n],data:save_data[req.params.n],count:req.params.n});
}
});
/***************************************************************/
/*****************************post****************************/
app.post("/compile",function(req,res){
console.log(req.body.submit);
//selecting compile file and run file name according to selected Language
let compile_file;
let run_file;
if(req.body.lang==1){
compile_file="code.cpp"
run_file="output1";
}
else if(req.body.lang==2){
compile_file="code.c"
run_file="output2";
}
//writing code to be executed in appropriate file
fs.writeFile(compile_file,req.body.code,function(err){
//if failed in storing file
if(err){
console.log(err);
}
//if successfull at storing code to be executed
else{
//saving the code;
console.log("Success writing");
if(req.body.submit=="1"){
console.log(req.body.code);
save_name.push(req.body.save);
save_data.push(req.body.code);
save_count+=1;
file_number=save_count-1;
file_name=req.body.save;
console.log("File number : "+file_number);
}
if(req.body.submit=="2"){
console.log(req.body.code);
save_data[file_number]=req.body.code;
save_name[file_number]=req.body.save;
file_name=req.body.save;
console.log("File number : "+file_number);
}
if(req.body.submit=="0"){
file_number="-1";
console.log("File number : "+file_number);
}
// selecting cmd to compile code according to language
let exec_file;
if(req.body.lang==1){
exec_file="g++ -o output1 code.cpp";
}
else if(req.body.lang==2){
exec_file="gcc -o output2 code.c";
}
//executing cmd to compile code
childProcess.exec(exec_file, (error, stdout, stderr) => {
//if failed at compilation
if (error) {
res.render("index",{ code:req.body.code,compiled: 2,output: `${error}`,fileNo:file_number,fileName:file_name});
}
//if successfull at compilation
else{
console.log("compilation done!");
//running the generated executable
let cmd = "printf '" + req.body.customInput + "' | ./"+run_file;
childProcess.exec(cmd, (error, stdout, stderr) => {
//if failed at running the executable
if (error) {
console.error(`exec error: ${error}`);
return;
}
//if successfull at running the executable
console.log("output ran");
res.render("index",{ code:req.body.code,compiled: 1,output: `${stdout}`,fileNo:file_number,fileName:file_name});
});
}
});
}
});
console.log("request complete");
});
app.post("/edit/:fileNo",function(req,res){
res.render("index",{ code:save_data[req.params.fileNo],compiled: 0,output:"",fileNo:req.params.fileNo,fileName:save_name[req.params.fileNo]});
});
/**************************************************************/
/************************Listener*****************************/
app.listen(3000,function(){
console.log("Server is kicking!");
});