@@ -18,26 +18,31 @@ server.use(middlewares)
1818const createServer = ( option : anyObject , callback ?: Function ) => {
1919 let config = option . https
2020 config = / ^ ( b o o l e a n | n u m b e r ) $ / . test ( typeof config ) ? config && { } : config
21+ let currentServer
2122 if ( config instanceof Object ) {
2223 if ( typeof config . key !== 'string' || typeof config . cert !== 'string' || config . key . length + config . cert . length === 0 ) {
2324 config . key = fs . readFileSync ( path . join ( __dirname , 'ssl/key.pem' ) )
2425 config . cert = fs . readFileSync ( path . join ( __dirname , 'ssl/cert.pem' ) )
2526 console . log ( "正在使用默认的证书配置" )
2627 }
27- https . createServer ( config , server ) . listen ( option . port , function ( ) {
28+ currentServer = https . createServer ( config , server ) . listen ( option . port , function ( ) {
2829 console . log ( )
2930 console . log ( `已启动json-server服务器 https://localhost:${ option . port } ` )
3031 console . log ( )
3132 typeof callback == 'function' && callback ( ) ;
3233 } )
3334 } else {
34- server . listen ( option . port , ( ) => {
35+ currentServer = server . listen ( option . port , ( ) => {
3536 console . log ( )
3637 console . log ( `已启动json-server服务器 http://localhost:${ option . port } ` )
3738 console . log ( )
3839 typeof callback == 'function' && callback ( ) ;
3940 } )
4041 }
42+ currentServer . on ( 'error' , ( ...rest ) => {
43+ option . port ++
44+ createServer ( option , callback )
45+ } )
4146}
4247/**
4348 * 启动mock服务
@@ -257,6 +262,6 @@ const Server = (option: anyObject, callback?: Function) => {
257262 res . status ( 200 ) . jsonp ( body )
258263 }
259264 server . use ( router )
260- createServer ( option , callback ) ;
265+ createServer ( option , callback )
261266}
262267export default Server
0 commit comments