| 37 | } |
| 38 | |
| 39 | function KoaServer() { |
| 40 | this.httpServer = null; |
| 41 | this.httpsServer = null; |
| 42 | this.requestRecordMap = {}; // store all request data to the map |
| 43 | const self = this; |
| 44 | |
| 45 | /** |
| 46 | * log the request info, write as |
| 47 | */ |
| 48 | this.logRequest = function *(next) { |
| 49 | const headers = this.request.headers; |
| 50 | let key = this.request.protocol + '://' + this.request.host + nurl.parse(this.request.url).pathname; // remove param to get clean key |
| 51 | |
| 52 | // take proxy data with 'proxy-' + url |
| 53 | if (headers['via-proxy'] === 'true') { |
| 54 | key = PROXY_KEY_PREFIX + key; |
| 55 | } |
| 56 | |
| 57 | printLog('log request with key :' + key); |
| 58 | let body = this.request.body; |
| 59 | body = typeof body === 'object' ? JSON.stringify(body) : body; |
| 60 | |
| 61 | self.requestRecordMap[key] = { |
| 62 | headers, |
| 63 | body |
| 64 | }; |
| 65 | yield next; |
| 66 | }; |
| 67 | |
| 68 | this.start(); |
| 69 | } |
| 70 | |
| 71 | KoaServer.prototype.constructRouter = function () { |
| 72 | const router = KoaRouter(); |