11var express = require ( 'express' ) ;
22var path = require ( 'path' ) ;
33var favicon = require ( 'serve-favicon' ) ;
4- var logger = require ( 'morgan' ) ;
54var cookieParser = require ( 'cookie-parser' ) ;
65var bodyParser = require ( 'body-parser' ) ;
76var helmet = require ( 'helmet' ) ;
@@ -17,20 +16,16 @@ var account = require('./routes/account');
1716var users = require ( './routes/users' ) ;
1817var apps = require ( './routes/apps' ) ;
1918var AppError = require ( './core/app-error' ) ;
19+ var log4js = require ( 'log4js' ) ;
20+ var log = log4js . getLogger ( "app" ) ;
2021var app = express ( ) ;
2122app . use ( helmet ( ) ) ;
2223app . disable ( 'x-powered-by' ) ;
2324// view engine setup
2425app . set ( 'views' , path . join ( __dirname , 'views' ) ) ;
2526app . set ( 'view engine' , 'pug' ) ;
2627
27- // uncomment after placing your favicon in /public
28- //app.use(favicon(path.join(__dirname, 'public', 'favicon.ico')));
29- if ( app . get ( 'env' ) === 'development' ) {
30- app . use ( logger ( 'dev' ) ) ;
31- } else if ( app . get ( 'env' ) === 'production' ) {
32- app . use ( logger ( 'combined' , { skip : function ( req , res ) { return res . statusCode < 400 } } ) ) ;
33- }
28+ app . use ( log4js . connectLogger ( log4js . getLogger ( "http" ) , { level : log4js . levels . INFO } ) ) ;
3429
3530app . use ( bodyParser . json ( ) ) ;
3631app . use ( bodyParser . urlencoded ( { extended : false } ) ) ;
@@ -39,22 +34,42 @@ app.use(express.static(path.join(__dirname, 'public')));
3934
4035//use nginx in production
4136if ( app . get ( 'env' ) === 'development' ) {
37+ log . debug ( "set Access-Control Header" ) ;
4238 app . all ( '*' , function ( req , res , next ) {
4339 res . header ( "Access-Control-Allow-Origin" , "*" ) ;
4440 res . header ( "Access-Control-Allow-Headers" , "Origin, X-Requested-With, Content-Type, Accept, Authorization" ) ;
4541 res . header ( "Access-Control-Allow-Methods" , "PUT,POST,GET,PATCH,DELETE,OPTIONS" ) ;
42+ log . debug ( "use set Access-Control Header" ) ;
4643 next ( ) ;
4744 } ) ;
4845}
4946
50- if ( _ . get ( config , 'common.storageType' ) === 'local'
51- && _ . get ( config , 'local.storageDir' )
52- ) {
53- if ( ! fs . existsSync ( _ . get ( config , 'local.storageDir' ) ) ) {
54- var dir = _ . get ( config , 'local.storageDir' ) ;
55- throw new Error ( `Please create dir ${ dir } ` ) ;
47+ log . debug ( "config common.storageType value: " + _ . get ( config , 'common.storageType' ) ) ;
48+
49+ if ( _ . get ( config , 'common.storageType' ) === 'local' ) {
50+ var localStorageDir = _ . get ( config , 'local.storageDir' ) ;
51+ if ( localStorageDir ) {
52+
53+ log . debug ( "config common.storageDir value: " + localStorageDir ) ;
54+
55+ if ( ! fs . existsSync ( localStorageDir ) ) {
56+ var e = new Error ( `Please create dir ${ localStorageDir } ` ) ;
57+ log . error ( e ) ;
58+ throw e ;
59+ }
60+ try {
61+ log . debug ( 'checking storageDir fs.W_OK | fs.R_OK' ) ;
62+ fs . accessSync ( localStorageDir , fs . W_OK | fs . R_OK ) ;
63+ log . debug ( 'storageDir fs.W_OK | fs.R_OK is ok' ) ;
64+ } catch ( e ) {
65+ log . error ( e ) ;
66+ throw e ;
67+ }
68+ log . debug ( "static download uri value: " + _ . get ( config , 'local.public' , '/download' ) ) ;
69+ app . use ( _ . get ( config , 'local.public' , '/download' ) , express . static ( localStorageDir ) ) ;
70+ } else {
71+ log . error ( 'please config local storageDir' ) ;
5672 }
57- app . use ( _ . get ( config , 'local.public' , '/download' ) , express . static ( _ . get ( config , 'local.storageDir' ) ) ) ;
5873}
5974
6075app . use ( '/' , routes ) ;
@@ -75,23 +90,26 @@ if (app.get('env') === 'development') {
7590 message : err . message ,
7691 error : err
7792 } ) ;
78- console . error ( err . stack ) ;
93+ log . error ( err ) ;
7994 } ) ;
8095 app . use ( function ( err , req , res , next ) {
8196 if ( err instanceof AppError . AppError ) {
8297 res . send ( err ) ;
98+ log . debug ( err ) ;
8399 } else {
84100 res . status ( err . status || 500 ) ;
85101 res . render ( 'error' , {
86102 message : err . message ,
87103 error : err
88104 } ) ;
89- console . error ( err . stack ) ;
105+ log . error ( err ) ;
90106 }
91107 } ) ;
92108} else {
93109 app . use ( function ( req , res , next ) {
94- res . status ( 404 ) . send ( new AppError . NotFound ( ) ) ;
110+ var e = new AppError . NotFound ( ) ;
111+ res . status ( 404 ) . send ( e ) ;
112+ log . debug ( e ) ;
95113 } ) ;
96114 // production error handler
97115 // no stacktraces leaked to user
@@ -103,7 +121,7 @@ if (app.get('env') === 'development') {
103121 var error = new AppError . AppError ( `服务器繁忙,请稍后再试!` ) ;
104122 error . status = status ;
105123 res . status ( status ) . send ( error ) ;
106- console . error ( err . stack ) ;
124+ log . error ( err ) ;
107125 }
108126 } ) ;
109127}
0 commit comments