-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathserver.js
More file actions
58 lines (46 loc) · 1.87 KB
/
Copy pathserver.js
File metadata and controls
58 lines (46 loc) · 1.87 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
// https://nodejs.org/en/docs/guides/anatomy-of-an-http-transaction/
const fs = require('fs');
const http = require('http')
// Start server
const server = http.createServer((req, res) => {
const { headers, method, url } = req;
let body = [];
req.on('error', (err) => {
console.error('Request error: ', err);
}).on('data', (chunk) => {
body.push(chunk);
}).on('end', () => {
body = Buffer.concat(body).toString();
console.log('Body', body); // print body for debugging
// at this point, `body` has the entire request body stored in it as a string
var prefix = '#include <iostream>\n#include <taskflow/taskflow.hpp>\nint main(){\n';
var code = prefix + body + '\n}';
// Remove stray characters
code = code.replace(/[\302]/g,' ');
code = code.replace(/[\240]/g,' ');
// Save the content to debug.cpp
fs.writeFile("./debug.cpp", code, function(err) {
if(err) {
return console.log("Output file error: ", err);
}
console.log("The file was saved in debug.cpp");
});
// spawn process to compile/execute debug.cpp & generate image
var exec = require('child_process').exec;
var child = exec("g++-mp-8 -std=c++17 -I . ./debug.cpp -o debug -pthread && ./debug > debug.dot && dot -Tpng ./debug.dot -o ./debug.png");
child.stderr.on('data', function(data) {
// Print error if spawn fails
console.log("Spawn error = ", data);
});
child.on('close', function() {
// Send image back to front end
res.setHeader("Access-Control-Allow-Origin", "*"); // This line avoids the CROS restriction
res.writeHead(200, {'Content-Type': 'image/png' });
var img = fs.readFileSync('./debug.png');
res.end(img, 'binary');
});
});
// console.log('Method', method);
// console.log('Url', url);
}).listen(3000);
console.log('Listening on port 3000');