@@ -2,19 +2,14 @@ const { createSecureServer } = require("http2");
2
2
const { readFileSync } = require ( "fs" ) ;
3
3
const { join } = require ( "path" ) ;
4
4
const Koa = require ( "koa" ) ;
5
- const logger = require ( "koa-logger " ) ;
5
+ const morgan = require ( "koa-morgan " ) ;
6
6
const Router = require ( "koa-router" ) ;
7
7
const rotatingFileStream = require ( "rotating-file-stream" ) ;
8
8
9
9
const PORT = process . env . PORT || 8443 ;
10
10
11
- const accessLogStream = rotatingFileStream (
12
- `access.log` ,
13
- {
14
- interval : `1d` , //rotate daily
15
- path : join ( __dirname , `log` )
16
- }
17
- ) ;
11
+ const accessLogStream = getLogStream ( `access.log` ) ;
12
+ const errorLogStream = getLogStream ( `error.log` ) ;
18
13
19
14
const serverOptions = {
20
15
key : readFileSync ( `${ __dirname } /ssl/selfsigned.key` ) ,
@@ -25,30 +20,37 @@ const app = new Koa();
25
20
const router = new Router ( ) ;
26
21
27
22
router . get ( "/" , ( ctx , next , ...args ) => {
28
- console . log ( "#get /" ) ;
29
23
// ctx.router available
30
24
ctx . body = "hellooo" ;
31
25
} ) ;
32
26
33
27
app
34
- . use ( logger ( {
35
- transporter : ( str , args ) => {
36
- if ( args . status < 400 ) {
37
- process . stdout . write ( str ) ;
38
- } else {
39
- process . stderr . write ( str ) ;
40
- }
41
- process . stdout . write ( `\n` ) ;
42
-
43
- accessLogStream . write ( str ) ;
44
- accessLogStream . write ( `\n` ) ;
45
- }
28
+ . use ( morgan ( `dev` , {
29
+ skip : ( req , res ) => res . statusCode < 400
30
+ } ) )
31
+ . use ( morgan ( `common` , {
32
+ stream : accessLogStream
33
+ } ) )
34
+ . use ( morgan ( `combined` , {
35
+ stream : errorLogStream ,
36
+ skip : ( req , res ) => res . statusCode < 400
46
37
} ) )
47
38
. use ( router . routes ( ) )
48
39
. use ( router . allowedMethods ( ) ) ;
49
40
41
+ console . info ( `Starting server @${ PORT } ` ) ;
42
+
50
43
createSecureServer ( serverOptions , app . callback ( ) )
51
44
. listen ( PORT ) ;
52
45
53
46
47
+ function getLogStream ( outputFilename ) {
48
+ return rotatingFileStream (
49
+ outputFilename ,
50
+ {
51
+ interval : `1d` , //rotate daily
52
+ path : join ( __dirname , `log` )
53
+ }
54
+ ) ;
55
+ }
54
56
0 commit comments