forked from BrodyHolden/battlesnake-node-snakenado
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
63 lines (51 loc) · 1.33 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
61
62
63
var config = require('./config.json');
var bodyParser = require('body-parser');
var express = require('express');
var logger = require('morgan');
var app = express();
var routes = require('./routes');
app.set('port', (process.env.PORT || config.port));
app.enable('verbose errors');
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(routes);
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);
}
return;
});
// 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 || 'no snakes here'
});
return;
});
// 500 handler middleware, respond with JSON only
app.use(function (err, req, res, next) {
var statusCode = err.status || 500;
res.status(statusCode);
res.send({
status: statusCode,
error: err
});
return;
});
var server = app.listen(app.get('port'), function () {
console.log('Server listening at http://%s:%s', config.host, app.get('port'));
});