I am trying to build an online C++ compiler in I am using react for frontend and Express for backend.
My server.js :
const express = require("express");
const app = express();
const cors = require("cors");
const fs = require("fs");
const exec = require("child_process").exec;
const PORT = process.env.PORT || 3001;
app.use(cors());
app.use(express.urlencoded({ extended: false }));
app.use(express.json());
app.use(express.static(__dirname + "/client/build"));
app.get("*", (req, res) => {
res.send({ server: "Running" });
});
app.post("/", (req, res) => {
var code = req.body.code;
var ip = req.body.ip;
fs.writeFile("./a.cpp", code, (err) => {});
fs.writeFile("./a.txt", ip, (err) => {});
exec("g++ a.cpp -o a", (err, stdout, stderr) => {
if (err) {
fs.writeFile("./a.cpp", "", (err) => {});
fs.writeFile("./a.txt", "", (err) => {});
return res.send({ stdout: stdout, stderr: stderr });
}
exec(".\a < a.txt", (err, stdout, stderr) => {
fs.writeFile("./a.cpp", "", (err) => {});
fs.writeFile("./a.txt", "", (err) => {});
return res.send({ stdout: stdout, stderr: stderr });
});
});
});
app.listen(PORT, () => {
console.log(`Server running at http://localhost:${PORT}`);
});
This is also deployed to heroku but the thing is It works fine when using locally on my machine but when I try to post the request to the server it says
/bin/sh: 1: g++: not found
I have added g++ as a dependency in Aptfile for my backend and it also compiles as follow while building
remote: -----> Apt app detected
remote: -----> Reusing cache
remote: -----> Updating apt caches
remote: Hit:1 http://apt.postgresql.org/pub/repos/apt focal-pgdg InRelease
remote: Hit:2 http://archive.ubuntu.com/ubuntu focal InRelease
remote: Get:3 http://archive.ubuntu.com/ubuntu focal-security InRelease [114 kB]
remote: Get:4 http://archive.ubuntu.com/ubuntu focal-updates InRelease [114 kB]
remote: Fetched 228 kB in 1s (310 kB/s)
remote: Reading package lists...
remote: -----> Fetching .debs for g++
remote: Reading package lists...
remote: Building dependency tree...
remote: 0 upgraded, 0 newly installed, 1 reinstalled, 0 to remove and 23 not upgraded.
but still I am getting the error. What is wrong with this?? Please help me to find the solution for this.