forked from Glitches/go-for-eat-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
71 lines (62 loc) · 1.63 KB
/
app.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
65
66
67
68
69
70
71
'use strict';
require('dotenv').config();
const koa = require('koa');
const helmet = require('koa-helmet');
const logger = require('koa-logger');
const cors = require('kcors');
const bodyParser = require('koa-bodyparser');
const compress = require('koa-compress');
const monk = require('monk');
const app = (module.exports = new koa());
const routes = require('./router.js');
const db = monk(process.env.MONGOLAB_URI);
//eslint-disable-next-line
db.then(()=> {console.log('Connected correctly to server')})
const User = db.get('users');
const Restaurant = db.get('restaurants');
const Raven = require('raven');
process.env.ENV === 'production' &&
Raven.config(process.env.SENTRY_DSN).install();
// Logger
app
.use(logger())
.use(helmet())
.use(cors())
.use(bodyParser())
// error handling
.use(async (ctx, next) => {
try {
await next();
} catch (err) {
ctx.body = undefined;
switch (ctx.status) {
case 401:
ctx.app.emit('error', err, this);
break;
case 400:
ctx.app.emit('Resource error', err, this);
break;
case 404:
ctx.app.emit('Resource not found', err, this);
break;
default:
if (err.message) {
ctx.body = { errors: [err.message] };
}
ctx.app.emit('error', err, this);
}
}
});
routes(app);
// Compress
app.use(compress());
// Raven
app.on('error', async err => {
console.error(err);
process.env.ENV === 'production' &&
await Raven.captureException(err, (err, eventId) => {
//eslint-disable-next-line
console.log(`Reported error ${eventId}`);
});
});
module.exports = app;