@@ -4,7 +4,6 @@ function _defineProperty(obj, key, value) { if (key in obj) { Object.definePrope
4
4
5
5
var connect = require ( 'connect' ) ,
6
6
fs = require ( 'fs' ) ,
7
- os = require ( 'os' ) ,
8
7
http = require ( 'http' ) ,
9
8
https = require ( 'https' ) ,
10
9
serveStatic = require ( 'serve-static' ) ,
@@ -24,13 +23,13 @@ exports.setOptions = function (optimist) {
24
23
optimist . alias ( 'p' , 'port' ) ;
25
24
optimist . describe ( 'p' , '端口' ) ;
26
25
optimist . alias ( 'x' , 'proxy' ) ;
27
- optimist . describe ( 'x' , '启用proxy代理服务 ' ) ;
26
+ optimist . describe ( 'x' , '开启 proxy 代理服务 ' ) ;
28
27
optimist . alias ( 'm' , 'middlewares' ) ;
29
28
optimist . describe ( 'm' , '加载项目中间件' ) ;
30
29
optimist . alias ( 'a' , 'all' ) ;
31
- optimist . describe ( 'a' , '整体编译 ' ) ;
30
+ optimist . describe ( 'a' , '使用整体编译模式 ' ) ;
32
31
optimist . alias ( 's' , 'https' ) ;
33
- optimist . describe ( 's' , '使用https协议 ' ) ;
32
+ optimist . describe ( 's' , '开启 https 服务 ' ) ;
34
33
} ;
35
34
36
35
exports . run = function ( options ) {
@@ -373,37 +372,31 @@ exports.run = function (options) {
373
372
374
373
app . use ( serveIndex ( cwd ) ) ;
375
374
376
- var server = void 0 ;
375
+ var servers = [ ] ;
377
376
378
- if ( ! isHttps ) {
379
- server = http . createServer ( app ) ;
380
- } else {
377
+ servers . push ( extend ( http . createServer ( app ) , { _port : port } ) ) ;
378
+ if ( isHttps ) {
381
379
var httpsOpts = {
382
380
key : fs . readFileSync ( sysPath . join ( __dirname , '../config/https/server.key' ) ) ,
383
381
cert : fs . readFileSync ( sysPath . join ( __dirname , '../config/https/server.crt' ) )
384
382
} ;
385
- server = https . createServer ( httpsOpts , app ) ;
383
+ servers . push ( extend ( https . createServer ( httpsOpts , app ) , { _port : '443' , _isHttps : true } ) ) ;
386
384
}
387
385
388
- server . on ( 'error' , function ( e ) {
389
- if ( e . code === 'EACCES' ) {
390
- warn ( '权限不足, 请使用sudo/管理员模式执行' ) ;
391
- } else if ( e . code === 'EADDRINUSE' ) {
392
- warn ( '端口 ' + port + ' 已经被占用, 请关闭占用该端口的程序或者使用其它端口.' ) ;
393
- }
394
- process . exit ( 1 ) ;
395
- } ) ;
396
- server . listen ( port , function ( ) {
397
- log ( 'Starting up server, serving at: ' + options . cwd ) ;
398
-
399
- var networkInterfaces = os . networkInterfaces ( ) ;
400
- var protocol = options . https ? 'https://' : 'http://' ;
401
- Object . keys ( networkInterfaces ) . forEach ( function ( dev ) {
402
- networkInterfaces [ dev ] . forEach ( function ( details ) {
403
- if ( details . family === 'IPv4' && details . address . indexOf ( '127.0.0.1' ) > - 1 ) {
404
- log ( 'Available on: ' + ( protocol + details . address + ':' + port ) . underline ) ;
405
- }
406
- } ) ;
386
+ servers . forEach ( function ( server ) {
387
+ server . on ( 'error' , function ( e ) {
388
+ if ( e . code === 'EACCES' ) {
389
+ warn ( '权限不足, 请使用sudo/管理员模式执行' ) ;
390
+ } else if ( e . code === 'EADDRINUSE' ) {
391
+ warn ( '端口 ' + server . _port + ' 已经被占用, 请关闭占用该端口的程序或者使用其它端口.' ) ;
392
+ }
393
+ process . exit ( 1 ) ;
394
+ } ) ;
395
+ server . listen ( server . _port , function ( ) {
396
+ var serverUrl = ( server . _isHttps ? 'https' : 'http' ) + '://127.0.0.1:' + server . _port ;
397
+
398
+ ! server . _isHttps && log ( 'Starting up server, serving at: ' + options . cwd ) ;
399
+ log ( 'Available on: ' + serverUrl . underline ) ;
407
400
} ) ;
408
401
} ) ;
409
402
0 commit comments