-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
56 lines (52 loc) · 1.61 KB
/
index.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
const express = require('express');
const app = express();
const port = 3000;
const fs = require('fs');
const path = require('path');
const spawn = require('child_process').spawn;
const bodyParser = require('body-parser');
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
app.use(express.static(path.join(__dirname, 'public')));
https: app.get('/', (req, res) => {
res.sendFile(__dirname + '/main.html');
});
app.post('/test', (req, res) => {
var source = req.body.code.split(/\r\n|\r\n/).join('\n');
var file = 'test.js';
fs.writeFile(file, source, 'utf8', function (error) {
console.log('write end');
});
var fail = [];
var success = '';
var compile = spawn('node', [file]);
compile.stdout.on('data', function (data) {
var str = data.toString().split('\r');
var good = '';
str.map(function (e, i) {
good += e;
});
success += good;
});
compile.stderr.on('data', function (data) {
console.log('stderr: ' + data);
var str = data.toString().split('\r\n');
var err = [];
str.map(function (e, i) {
if (i > 3) err.push(e);
});
console.log(err);
fail = fail.concat(err);
});
compile.on('exit', code => {
console.log(`process has finished and exited with code: ${code}`);
console.log(success);
res.json({
success: success.toString('utf8'),
fail: fail.toString('utf8'),
});
});
});
app.listen(port, () => {
console.log(`Example app listening at http://localhost:${port}`);
});