-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
42 lines (34 loc) · 977 Bytes
/
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
const crypto = require('crypto');
const {execSync} = require('child_process')
const express = require('express')
const fs = require('fs');
const app = express()
app.use(express.static('public'));
app.use(function(req, res, next) {
req.rawBody = ''
req.setEncoding('utf8')
req.on('data', function(chunk) {
req.rawBody += chunk
})
req.on('end', function() {
next();
});
});
const port = 3000
app.put('/compile', (req, res) => {
const hash = crypto.createHash('md5').update(req.rawBody).digest('hex');
const path = `compiled/${hash}.js`
if (!fs.existsSync(path)) {
const tmpFilePath = `tmp/${hash}.cpp`
fs.writeFileSync(tmpFilePath, req.rawBody, err => {
if (err) {
console.error(err);
}
})
execSync(`npm run compile -- ${tmpFilePath}`)
}
res.send(path)
})
app.listen(port, () => {
console.log(`Listening at http://localhost:${port}`)
})