-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.js
96 lines (82 loc) · 2.31 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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
var thunkify = require('thunkify');
var ClientError = require('./src/error/client');
var koa = require('koa');
koa.static = require('koa-static');
var reqlog = require('koa-logger');
koa.logger = function (assets) {
var lg = reqlog();
if (!assets) {
return function *(next) {
if (/(\.js|\.css|\.ico|\.png)$/.test(this.path)) {
yield next;
} else {
yield lg.call(this, next);
}
}
} else {
return lg;
}
};
koa.session = require('koa-generic-session');
koa.route = require('koa-route');
koa.bodyparser = require('koa-bodyparser');
koa.views = require('koa-views');
koa.locale = require('koa-locale');
koa.i18n = require('koa-i18n');
koa.flash = require('koa-flash');
koa.qs = require('koa-qs');
koa.cache = require('koa-cash');
var config = require('./config/config');
var routes = require('./src/routes');
var app = koa();
app.name = config.name;
app.keys = config.keys;
app.env = config.env;
app.config = config;
app.use(function *(next) {
try {
yield next;
} catch (e) {
throw e;
if (e instanceof ClientError) {
this.status = 200;
this.body = {
error: {
message: e.message,
code: e.code
}
};
} else {
this.status = 500;
if (this.app.env == 'dev') {
// this.body = e;
throw e;
} else {
this.body = 'internal server error'; // TODO: render pretty 500 error
}
}
}
});
koa.qs(app);
app.use(koa.logger());
app.use(function *(next) {
if (this.isAjax !== undefined) {
this.isAjax = this.header['x-requested-with'] && this.header['x-requested-with'].toLowerCase() === 'xmlhttprequest';
}
yield next;
});
app.use(koa.bodyparser(config.bodyparser));
app.use(koa.static(config.static.directory, config.static));
koa.locale(app);
app.use(koa.i18n(app, config.i18n));
app.use(koa.session(config.session));
app.use(koa.views(config.view.directory, config.view));
app.use(koa.flash(config.flash));
routes(app);
if (!module.parent) {
app.listen(config.port, function () {
console.log('Server running on port ' + config.port)
});
} else {
module.exports = app;
}