Không biết bạn sử dụng server Windows hay Linux? Nếu là server linux bạn có thể thử các bước sau:
- Dùng fs.writeFile để tạo file cpp từ nội dụng trên website, sau đó ghi lên server.
- Dùng child process (reference) thực thi lệnh g++ để compile ra file runable.
- Dùng child process như trên để thực thi file runable vừa compile ra.
Tuy nhiên bạn cần phải cẩn thận vì đây là 1 lỗ hổng lớn đối với hệ thống đấy nhé
Ví dụ:
const { execFile } = require('child_process');
const child = execFile('g++', ['hello.cpp','-o','hello'], (error, stdout, stderr) => {
if (error) {
throw error;
}
console.log(stdout);
const child = execFile('./hello', (error, stdout, stderr) => {
if (error) {
throw error;
}
console.log(stdout);
});
});