-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
executable file
·59 lines (48 loc) · 1.36 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
const os = require('os');
const Koa = require('koa');
const bodyParser = require('koa-bodyparser');
const app = new Koa();
app.proxy = true;
if (process.env.NODE_ENV !== 'production') {
app.use(async (ctx, next) => {
await next();
const rt = ctx.response.get('X-Response-Time');
console.log(`${ctx.method} ${ctx.url} - ${rt}`);
});
}
// 允许跨域
app.use(async (ctx, next) => {
ctx.set('Access-Control-Allow-Origin', '*');
ctx.set('Access-Control-Allow-Methods', 'POST, GET, OPTIONS, DELETE, PATCH');
ctx.set('Access-Control-Max-Age', '3600');
ctx.set('Access-Control-Allow-Headers', 'x-requested-with,Authorization,Content-Type,Accept');
ctx.set('Access-Control-Allow-Credentials', 'true');
if (ctx.method === 'OPTIONS') {
ctx.status = 200;
}
await next();
});
// 记录响应时长
app.use(async (ctx, next) => {
const start = new Date();
await next();
const ms = Date.now() - start;
ctx.set('X-Response-Node', os.hostname());
ctx.set('X-Response-Time', `${ms}ms`);
});
// 捕获异常
app.use(async (ctx, next) => {
try {
await next();
} catch (err) {
console.error(err);
ctx.status = err.status || 500;
ctx.body = err.message;
ctx.app.emit('error', err, ctx);
}
});
const router = require('./router');
app.use(bodyParser());
app.use(router.routes());
app.use(router.allowedMethods());
module.exports = app;