server.js
1.06 KB
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
import path from 'path';
import Koa from 'koa';
import importDir from 'import-dir';
import json from 'koa-json';
import bodyParser from 'koa-bodyparser';
import qs from 'koa-qs';
import morgan from 'koa-morgan';
import FileStreamRotator from 'file-stream-rotator';
import mkdirp from 'mkdirp';
import {port, logDirectory} from './config';
import logger from './utils/logger';
logger.info('Hello');
// 创建日志目录
const logDir = path.join(process.cwd(), logDirectory);
mkdirp.sync(logDir);
// create a rotating write stream
const accessLogStream = FileStreamRotator.getStream({
date_format: 'YYYYMMDD',
filename: logDir + '/access-%DATE%.log',
frequency: 'daily',
verbose: false,
});
const app = new Koa();
qs(app);
app.use(morgan('combined', {stream: accessLogStream}));
app.use(bodyParser());
app.use(json({pretty: true}));
// 导入路由
const routes = importDir('./routes');
Object.keys(routes).map(
(name) => app.use(routes[name].routes())
);
app.listen(port, (err) => {
if (err) {
throw err;
}
console.log('✅ koa listening on port %s', port);
});