-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
62 lines (50 loc) · 1.41 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
const Koa = require('koa')
const convert = require('koa-convert');
const koaLogger = require('koa-logger');
const helmet = require('koa-helmet');
const bodyParser = require('koa-bodyparser');
const static = require('koa-static');
const view = require('koa-views');
const path = require('path');
const router = require('./app/router');
const app = new Koa();
const axios = require('axios');
const getAccessToken = require('./init').getAccessToken;
//错误处理
app.use(async (ctx, next) => {
try {
await next();
} catch (e) {
ctx.response.status = e.statusCode || e.status || 500;
ctx.response.body = {
message: err.message
};
ctx.app.emit('error', err, ctx);
}
});
app.on('error', (err, ctx) => {
console.error('server error', err);
});
app.use(convert(koaLogger()));
app.use(bodyParser());
app.use(helmet());
const staticPath = './../static';
const viewsPath = './views';
app.use(static(
path.join(__dirname, staticPath)
));
app.use(view(path.join(__dirname, viewsPath), {
extension: 'ejs'
}));
app.use(router.routes()).use(router.allowedMethods());
getAccessToken().then(data => {
global.accessTokens = data;
console.log(global.accessTokens, 'app.js')
});
setInterval(() => {
getAccessToken().then(data => {
global.accessTokens = data;
});
}, 1.5 * 60 * 60 * 1000);
app.listen(3000);
console.log(`项目启动 at port 3000`);