-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
60 lines (50 loc) · 1.32 KB
/
index.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
var bodyParser = require('body-parser')
var express = require('express')
var logger = require('morgan')
var snake = require('./snake');
var app = express()
app.set('port', (process.argv[2] || 9001))
app.enable('verbose errors')
app.use(logger('dev'))
app.use(bodyParser.json())
// Handle POSTs to start and move
app.post('/start', snake.start);
app.post('/move', snake.move);
app.use('*', function (req, res, next) {
if (req.url === '/favicon.ico') {
// Short-circuit favicon requests
res.set({'Content-Type': 'image/x-icon'})
res.status(200)
res.end()
next()
} else {
// Reroute all 404 routes to the 404 handler
var err = new Error()
err.status = 404
next(err)
}
})
// 404 handler middleware, respond with JSON only
app.use(function (err, req, res, next) {
if (err.status !== 404) {
return next(err)
}
res.status(404)
res.send({
status: 404,
error: err.message || "These are not the snakes you're looking for"
})
})
// 500 handler middleware, respond with JSON only
app.use(function (err, req, res, next) {
var statusCode = err.status || 500
console.error(err.stack)
res.status(statusCode)
res.send({
status: statusCode,
error: err
})
})
var server = app.listen(app.get('port'), function () {
console.log('Server listening on port %s', app.get('port'))
})