-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.ts
81 lines (69 loc) · 1.85 KB
/
server.ts
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
if (process.env.NODE_ENV !== 'production') {
require('dotenv').config();
}
import * as Koa from 'koa';
import * as log4js from 'log4js';
import * as bodyParser from 'koa-bodyparser';
import * as hbs from 'koa-hbs';
import {mongodb} from './src/models/Iridium';
// import index from './routes/index';
import upload from './src/routes/upload';
// import users from './src/routes/users';
import pack from './src/routes/package';
import apps from './src/routes/app';
// import packages from './routes/packages';
const logger = log4js.getLogger();
const app = new Koa();
app.use(hbs.middleware({
viewPath: __dirname + '/views',
}));
app.use(async (ctx, next) => {
const start = new Date();
await next();
const ms = Date.now() - start.getTime();
ctx.set('X-Response-Time', `${ms}ms`);
});
// 错误处理`
app.use(async (ctx, next) => {
try {
await next();
} catch (err) {
// will only respond with JSON
console.log(err);
ctx.status = err.status || 500;
ctx.body = {
message: err.message,
};
if (err.errCode) {
ctx.body['errCode'] = err.errCode;
}
if (ctx.response.status >= 500) {
logger.error(err);
} else if (ctx.response.status >= 400) {
logger.warn(err);
}
}
});
// 跨域
app.use(async (ctx, next) => {
ctx.set('Access-Control-Allow-Origin', '*');
ctx.set('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE, PATCH');
ctx.set('Access-Control-Allow-Headers', 'Content-Type, X-Requested-With',);
if (ctx.method === 'OPTIONS') {
ctx.status = 204;
} else {
await next();
}
});
app.use(bodyParser());
// app.use(index.routes());
// app.use(users.routes());
app.use(apps.routes());
app.use(upload.routes());
app.use(pack.routes());
// app.use(packages.routes());
mongodb.connect().then(() => {
app.listen(8001, () => {
console.log('app listening port 8001');
});
});