-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
54 lines (41 loc) · 1.11 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
const express = require('express')
const app = express()
const port = 3000
app.use(express.static('public'))
app.use(express.json())
var cors = require('cors')
app.use(cors())
// express osaa hakea index.html ilman tätä
//app.get('/', (req, res) => console.log("get"))
app.post('/move', function (req, res) {
let position = req.body.position
let depth = req.body.depth
if (depth > 4) {
res.status(400).send("Depth too deep")
return false
}
requestMove(position, depth, (move) => {
if (move === "error") {
res.status(400)
} else {
res.send(move)
}
})
})
app.listen(process.env.PORT || port, () => console.log(`Example app listening on port ${port}!`))
function requestMove(pos, depth, callback) {
const {spawn} = require('child_process');
if (process.platform === "win32") {
python = spawn('python', ["main.py", pos, depth])
} else {
python = spawn('python3', ["main.py", pos, depth])
}
let move = ""
python.stdout.on('data', (data) => {
move = JSON.parse(data)
callback(move)
})
python.stderr.on('data', () => {
callback("error")
})
}