-
Notifications
You must be signed in to change notification settings - Fork 2
/
router.js
64 lines (53 loc) · 1.36 KB
/
router.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
59
60
61
62
63
64
const express = require("express");
const validate = require("./replay-validate");
const httpStatus = require("http-status");
const router = express.Router();
router.get("/replay", function(req, res) {
res.status(405).render("error", {
error_code: res.statusCode,
error_summary: httpStatus[res.statusCode],
error_message: null
});
});
router.post("/replay", function(req, res) {
if (req.body.json === undefined) {
replay = req.body;
}
else {
replay = toJsonOrNull(req.body.json);
}
if (!(replay instanceof Object) || Object.keys(replay).length === 0) {
throw {
"type": "CustomError",
"statusCode": 400,
"msg": "Invalid JSON"
};
}
else {
validate(replay, function(err) {
if (err) {
throw {
"type": "CustomError",
"statusCode": 400,
"msg": err
}
}
else {
res.render("index", {
replay: JSON.stringify(replay, null, 0)
});
}
});
}
});
router.get("/", function(req, res) {
res.render("main");
});
function toJsonOrNull(str) {
try {
return JSON.parse(str);
} catch (e) {
return null;
}
}
module.exports = router;