-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathserver.js
More file actions
121 lines (107 loc) · 4.03 KB
/
server.js
File metadata and controls
121 lines (107 loc) · 4.03 KB
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
const express = require('express');
const bodyParser = require('body-parser');
const { exec } = require('child_process');
const fs = require('fs');
const cors = require('cors');
const multer = require('multer');
const tesseract = require('tesseract.js');
const path = require('path');
const app = express();
const port = 5000;
// Enable CORS and JSON body parsing
app.use(cors());
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
// Setup file upload middleware using multer
const upload = multer({ dest: 'uploads/' });
// Root route for testing server
app.get('/', (req, res) => {
res.send('Welcome to the code runner server!');
});
// POST route to run code
app.post('/run-code', upload.single('file'), (req, res) => {
const { code: inputCode, language } = req.body;
const file = req.file;
if (file) {
// If an image is uploaded, use OCR to extract code
tesseract.recognize(
path.join(__dirname, file.path),
'eng'
)
.then(({ data: { text } }) => {
fs.unlinkSync(file.path); // Clean up uploaded file
processCode(text.trim(), language, res); // Process the extracted code
})
.catch(err => {
console.error('OCR Error:', err);
fs.unlinkSync(file.path); // Clean up uploaded file
res.status(500).json({ error: 'Error in OCR processing' });
});
} else if (inputCode) {
processCode(inputCode, language, res); // Process the code directly
} else {
res.status(400).json({ error: 'No code or file provided.' });
}
});
// Function to handle code compilation and execution
const processCode = (code, language, res) => {
let tempFile = '';
let compileCommand = '';
let runCommand = '';
const isWindows = process.platform === 'win32'; // Check if the system is Windows
if (language === 'c') {
tempFile = 'temp.c';
compileCommand = `gcc ${tempFile} -o temp.exe`;
runCommand = isWindows ? 'temp.exe' : './temp.exe'; // Use temp.exe directly on Windows
} else if (language === 'cpp') {
tempFile = 'temp.cpp';
compileCommand = `g++ ${tempFile} -o temp.exe`;
runCommand = isWindows ? 'temp.exe' : './temp.exe'; // Use temp.exe directly on Windows
} else if (language === 'java') {
tempFile = 'Temp.java';
compileCommand = `javac ${tempFile}`;
runCommand = 'java Temp';
} else if (language === 'python') {
tempFile = 'temp.py';
runCommand = `python ${tempFile}`;
} else {
return res.status(400).json({ error: 'Unsupported language' });
}
fs.writeFileSync(tempFile, code); // Write code to temporary file
// Compile the code
if (compileCommand) {
exec(compileCommand, (err, stdout, stderr) => {
if (err || stderr) {
cleanUp(tempFile);
return res.status(500).json({ error: stderr || 'Compilation error' });
}
executeCode(runCommand, tempFile, res);
});
} else {
executeCode(runCommand, tempFile, res);
}
};
// Function to execute the code
const executeCode = (runCommand, tempFile, res) => {
exec(runCommand, (err, stdout, stderr) => {
cleanUp(tempFile);
if (fs.existsSync('temp.exe')) fs.unlinkSync('temp.exe'); // Remove the compiled executable
if (err || stderr) {
return res.status(500).json({ error: stderr || 'Runtime error' });
}
res.json({ output: stdout });
});
};
// Function to clean up temporary files
const cleanUp = (tempFile) => {
if (fs.existsSync(tempFile)) fs.unlinkSync(tempFile);
if (fs.existsSync('temp.exe')) fs.unlinkSync('temp.exe');
};
// Catch-all for undefined routes
app.use((req, res) => {
res.status(404).send('Route not found.');
});
// Start the server
app.listen(port, () => {
console.log(`Server running at http://localhost:${port}`);
});