-
Notifications
You must be signed in to change notification settings - Fork 2
/
server.js
67 lines (55 loc) · 1.42 KB
/
server.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
/**
* Created by cesarcruz on 3/10/15.
*/
require('dotenv').load();
var koa = require('koa'),
Api = require('./api'),
mount = require('koa-mount'),
db = require('./models'),
jwt = require('koa-jwt'),
cors = require('koa-cors');
session = require('koa-session');
var app = koa();
//CORS
app.use(cors({
origin : true,
methods : ['GET', 'POST', 'PUT', 'DELETE', 'OPTIONS'],
headers : ['Content-Type', 'Authorization']
}));
var api = Api(db);
app.keys = ['cesarsalad'];
app.use(session(app));
//Handle errors
app.use(function *(next){
try {
yield next;
} catch(err) {
switch(err.status){
case 404:
this.status = 404;
this.body = err.message;
break;
case 401:
this.status = 401;
this.body = err.message;
break;
case 400:
this.status = 400;
this.body = err.message;
break;
case 403:
this.status = 403;
this.body = err.message;
break;
case 500:
this.status = 500;
this.body = err.message;
break;
}
}
});
app.use(jwt({ secret : process.env.APP_JWT_SECRET , passthrough: true}));
app.use(mount(api));
app.listen(4567);
console.log('listening on port 4567');
exports.app = app;