-
Notifications
You must be signed in to change notification settings - Fork 0
/
handlers.js
62 lines (53 loc) · 1.29 KB
/
handlers.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
const fs = require("fs");
/**
* Don't worry about anything in this file,
* focus on writing your snake logic in index.js endpoints.
*/
const poweredByHandler = (req, res, next) => {
res.setHeader('X-Powered-By', 'Battlesnake')
next()
}
const fallbackHandler = (req, res, next) => {
console.dir(req.baseUrl)
// Root URL path
if (req.baseUrl === '') {
res.status(200)
let content = fs.readFileSync(`${__dirname}/logs/index.html`, "utf8");
return res.send(content)
}
// Short-circuit favicon requests
if (req.baseUrl === '/favicon.ico') {
res.set({'Content-Type': 'image/x-icon'})
res.status(200)
res.end()
return next()
}
// Reroute all 404 routes to the 404 handler
const err = new Error()
err.status = 404
return next(err)
}
const notFoundHandler = (err, req, res, next) => {
if (err.status !== 404) {
return next(err)
}
res.status(404)
return res.send({
status: 404,
error: err.message || "These are not the snakes you're looking for",
})
}
const genericErrorHandler = (err, req, res, next) => {
const statusCode = err.status || 500
res.status(statusCode)
return res.send({
status: statusCode,
error: err,
})
}
module.exports = {
fallbackHandler,
notFoundHandler,
genericErrorHandler,
poweredByHandler,
}