-
Notifications
You must be signed in to change notification settings - Fork 4
/
app.js
54 lines (44 loc) · 1.6 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
global.__base = __dirname + '/'; // 设置全局require目录前缀
require('best-require')(process.cwd()); // 添加require里的~/功能
const Koa = require('koa');
const app = new Koa();
const views = require('koa-views');
// const json = require('koa-json')
require('~/core/prototype');
const bodyparser = require('koa-bodyparser'); // 获取post请求的参数
const {accessLogger, systemLogger, accessErrorLogger} = require('~/core/logger');
const icefire = require('~/core/icefire/');
const scheduleObj = require('~/core/schedule')(); // 定时任务
const compress = require('koa-compress'); // 压缩
const helmet = require('koa-helmet'); // 安全
const favicon = require('koa-favicon'); // favicon
// const routeEach = require(global.__base + 'core/routeEach');
const routeEach = require('~/core/routeEach');
app.use(bodyparser());
icefire(app);
app.use(accessLogger()); // 中间件
/*
* 压缩
* */
const options = {threshold: 2048};
app.use(compress(options));
app.use(helmet());
// app.use(json())
// 静态资源托管
app.use(require('koa-static-server')({rootDir: __dirname + '/public', rootPath: '/public'}));
// app.use(favicon(__dirname + '/public/favicon.ico'));
app.use(favicon(__dirname + '/public/logo.png'));
app.use(views(__dirname + '/views', {
extension: 'pug'
}));
routeEach(app, '/api'); // 路由地址
app.use((ctx, next) => {
ctx.errorCode('NOTFOUND');
ctx.response.status = 404;
});
// error-handling
app.on('error', (err, ctx) => {
systemLogger.error(err);
});
// app.on('error', err => {systemLogger.error(err); });
module.exports = app;