forked from birl-language/birl-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
49 lines (41 loc) · 1.44 KB
/
app.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
const app = require('express')();
const cors = require('cors');
const fs = require('fs');
const birl = require('./code_exec.js');
const verif = require('./code_verification.js');
var corsOptions = {
origin: 'https://birl-language.github.io'
};
// Permitindo CORS
app.use(cors(corsOptions));
app.get ('/', function (req, res) {
console.log ('-----------------------------------------');
console.log ('GET AT \'/\'');
res.send ('HELLO');
});
app.post('/compile', cors (corsOptions), function (req, res) {
console.log ('-----------------------------------------');
console.log ('POST AT \'/compile\'');
// Lendo o JSON
var body = '';
req.on('data', function (data) {
body += data;
});
// Enviando o código e o stdin para a execução do código
req.on('end', function () {
var json = JSON.parse(body);
if (json.code == null || verif(json.code)) {
res.setHeader('Content-Type', 'application/json');
res.end(JSON.stringify({ error: "ERRO DE COMPILAÇÃO, CUMPADI!!\n",
stdout: null,
}));
}
else
birl(json.code, json.stdin, res);
});
});
app.options('/compile', cors(corsOptions));
var port = process.env.PORT || 3000;
app.listen (port);
console.log ('-----------------------------------------');
console.log ('Listening at http://localhost:' + port);