-
Notifications
You must be signed in to change notification settings - Fork 6
/
app.js
62 lines (51 loc) · 1.3 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
'use strict';
// Init server
const koa = require('koa');
const app = module.exports = new koa();
const routes = require('./routes.js');
const User = require('./models/user.model');
require('./db');
// Dependencies
const bodyParser = require('koa-bodyparser');
const compress = require('koa-compress');
const cors = require('kcors');
const logger = require('koa-logger');
const cache = require('koa-redis-cache');
// Logger
app.use(logger());
app.use(cors());
app.use(bodyParser());
app.use(async (ctx, next) => {
try {
await next();
} catch (err) {
ctx.body = undefined;
switch (ctx.status) {
case 401:
ctx.app.emit('error', err, this);
break;
default:
if (err.message) {
ctx.body = {errors:[err.message]};
}
ctx.app.emit('error', err, this);
}
}
});
app.use(async (ctx, next) => {
if(!ctx.headers['authorization']) return await next();
let token = ctx.headers['authorization'].split(' ').pop();
if (!token) return await next();
ctx.user = await User.findOne({token});
return await next();
});
routes(app);
// Compress
app.use(compress());
// Run server
if (!module.parent) {
const ip = process.env.ip || 'localhost';
const port = process.env.port || 3000;
app.listen(port);
console.log(`Orbits server running at http://${ip}:${port}`);
}