-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathserver.js
More file actions
38 lines (30 loc) · 869 Bytes
/
server.js
File metadata and controls
38 lines (30 loc) · 869 Bytes
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
'use strict';
const koa = require('koa');
const app = koa();
const cors = require('koa-cors');
const staticServer = require('koa-static');
const api = require('./api');
const runtime = process.env.npm_config_env; // jshint ignore:line
const gzip = require('koa-gzip');
app.use(gzip());
if (!!runtime && runtime === 'dev') {
app.use(function *responseTimeMiddleware(next) {
const start = new Date();
yield next;
const ms = new Date() - start;
this.set('X-Response-Time', ms + 'ms');
});
app.use(function *URLCallResponsetime(next) {
const start = new Date();
yield next;
const ms = new Date() - start;
console.log(`${this.method} ${this.url} - ${ms}ms`);
});
}
// setup CORS
app.use(cors());
// hook our API into the stack
app.use(api);
// initialize the static server
app.use(staticServer('./public'));
app.listen(3000);