-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
84 lines (68 loc) · 1.63 KB
/
Copy pathserver.js
File metadata and controls
84 lines (68 loc) · 1.63 KB
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
const express = require('express');
const path = require('path');
const bodyParser = require('body-parser');
const MazeService = require('./src/service-maze')
const FileSystem = require('./service-file')
const app = express();
// app.use(bodyParser.json());
var jsonParser = bodyParser.json()
app.use(express.static(path.join(__dirname, 'build')));
app.get(
'/',
function (req, res) {
res.sendFile(
path.join(__dirname, 'build', 'index.html')
);
}
);
app.get(
'/api/maze/:id',
function (req, res) {
const mazeContent = FileSystem().getFile(req.params.id, __dirname)
res.json(
{
mazeName: req.params.id,
mazeContent: mazeContent
}
);
}
);
app.get(
'/api/mazes',
function (req, res) {
const mazes = FileSystem().getFiles(__dirname).map(f => {
return {
mazeName: f
}
})
res.json(mazes);
}
);
app.post(
'/api/maze',
jsonParser,
function (req, res) {
const { mazeName, mazeContent } = req.body;
try {
FileSystem().saveFile(mazeName, __dirname, mazeContent)
}
catch (e) {
console.error('Could not save file on server')
}
const parseObj = MazeService().parseMaze(req.body)
const start = parseObj.start
const end = parseObj.end
const { pathways, bestPathway } = MazeService().solveMaze(parseObj.maze)
res.json(
{
mazeName: mazeName,
mazeContent: mazeContent,
start: start,
end: end,
pathways: pathways,
bestPathway: bestPathway
}
);
}
);
app.listen(9000);