-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
58 lines (52 loc) · 1.72 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
54
55
56
57
58
const express = require('express');
const app = express();
const port = process.env.PORT || 5000;
const fs = require('fs');
const path = require('path');
const { exec, spawn } = require('child_process');
app.listen(port, () => console.log(`Listening on port ${port}`));
app.use(express.json());
app.use(express.static(path.join(__dirname, 'client/build')));
app.get('/', function(req, res) {
res.sendFile(path.resolve(__dirname, 'client/build', 'index.html'));
console.log("Root");
});
app.get('/express_backend', (req, res) => {
res.send({check_status: true});
});
app.post('/game_room', function (req, res) {
// let room_id = req.params.room_id;
const client_board = req.body;
console.log(client_board);
const filename = 'client/match_board/board.json';
if (!fs.existsSync(filename)) {
fs.writeFileSync(filename, JSON.stringify({
"board": client_board,
}));
res.send({
board: client_board,
init: true,
});
console.log("Not exists, done");
} else {
let file = fs.readFileSync(filename, 'utf-8');
cont = JSON.parse(file);
cont["board"] = client_board;
fs.writeFileSync(filename, JSON.stringify(cont));
res.send({
board: cont["board"],
init: false,
});
console.log("Exists, done");
}
let file = fs.readFileSync(filename, 'utf-8');
cont = JSON.parse(file);
console.log("Server board length: ", cont["board"].length);
});
app.delete('/game_room', function (req, res) {
const filename = 'client/match_board/board.json';
fs.unlinkSync(filename, function(err) {
if (err) throw err;
console.log("File deleted!");
});
});