-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
68 lines (55 loc) · 1.4 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
'use strict';
const Koa = require('koa');
const Router = require('koa-router');
const bodyParser = require('koa-bodyparser');
const decache = require('decache');
global.logger = require('@adenin/cf-logger');
const app = new Koa();
const router = new Router();
const PORT = process.env.PORT || 4000;
let index = require('./index');
router
.post('/:activity', async (ctx) => {
if (process.env.NODE_ENV === 'development') {
logger.debug('Decaching...');
decache('./index');
index = require('./index');
}
await index.activities(ctx);
})
.all('/:activity', async (ctx) => {
const err = new Error('Method not allowed');
err.status = 405;
ctx.app.emit('error', err, ctx);
})
.all('/', async (ctx) => {
const err = new Error('Not found');
err.status = 404;
ctx.app.emit('error', err, ctx);
});
app
.use(async (ctx, next) => {
try {
process.env.HOST = ctx.req.headers.host + ctx.req.url;
await next();
} catch (err) {
ctx.app.emit('error', err, ctx);
}
})
.use(bodyParser())
.use(router.routes())
.use(router.allowedMethods())
.on('error', (err, ctx) => {
logger.error(err);
ctx.status = err.status || 500;
ctx.body = {
Response: {
ErrorCode: ctx.status,
Data: {
ErrorText: err.message
}
}
};
})
.listen(PORT);
logger.info('Server running on port ' + PORT);