-
Notifications
You must be signed in to change notification settings - Fork 19
/
server.js
59 lines (50 loc) · 1.55 KB
/
server.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
const express = require("express");
const bodyParser = require("body-parser");
const path = require("path");
var request = require("request");
const axios = require("axios");
const app = express();
app.use(bodyParser.json());
const code = `#include <iostream>
using namespace std;
int main() {
int x=10;
int y=25;
int z=x+y;
cout<<"Sum of x+y = " << z;
}`;
// const proxyurl = "https://cors-anywhere.herokuapp.com/";
const url = "https://api.jdoodle.com/v1/execute";
app.post("/", (req, res) => {
console.log(req.body.lang);
const program = {
script: req.body.code,
stdin: req.body.input,
language: req.body.lang,
versionIndex: "0",
clientId: "YOUR_CLIENT_ID",
clientSecret: "YOUR_CLIENT_SECRET",
};
request(
{
url: url,
method: "POST",
json: program,
},
function (error, response, body) {
res.json(body);
console.log("error:", error);
console.log("statusCode:", response && response.statusCode);
console.log("body:", body);
}
);
});
// production
if (process.env.NODE_ENV === "production") {
app.use(express.static("app/build"));
app.get("*", (req, res) => {
res.sendFile(path.resolve(__dirname, "app", "build", "index.html"));
});
}
const port = process.env.PORT || 5000;
app.listen(port, () => console.log(`server started on port ${port}`));