@@ -1083,8 +1083,16 @@ var createServerHandle = exports._createServerHandle =
1083
1083
}
1084
1084
1085
1085
if ( address || port ) {
1086
- debug ( 'bind to ' + address ) ;
1087
- if ( addressType === 6 ) {
1086
+ debug ( 'bind to ' + ( address || 'anycast' ) ) ;
1087
+ if ( ! address ) {
1088
+ // Try binding to ipv6 first
1089
+ err = handle . bind6 ( '::' , port ) ;
1090
+ if ( err ) {
1091
+ handle . close ( ) ;
1092
+ // Fallback to ipv4
1093
+ return createServerHandle ( '0.0.0.0' , port ) ;
1094
+ }
1095
+ } else if ( addressType === 6 ) {
1088
1096
err = handle . bind6 ( address , port ) ;
1089
1097
} else {
1090
1098
err = handle . bind ( address , port ) ;
@@ -1214,7 +1222,7 @@ Server.prototype.listen = function() {
1214
1222
1215
1223
if ( arguments . length == 0 || util . isFunction ( arguments [ 0 ] ) ) {
1216
1224
// Bind to a random port.
1217
- listen ( self , '0.0.0.0' , 0 , null , backlog ) ;
1225
+ listen ( self , null , 0 , null , backlog ) ;
1218
1226
1219
1227
} else if ( arguments [ 0 ] && util . isObject ( arguments [ 0 ] ) ) {
1220
1228
var h = arguments [ 0 ] ;
@@ -1240,15 +1248,15 @@ Server.prototype.listen = function() {
1240
1248
util . isFunction ( arguments [ 1 ] ) ||
1241
1249
util . isNumber ( arguments [ 1 ] ) ) {
1242
1250
// The first argument is the port, no IP given.
1243
- listen ( self , '0.0.0.0' , port , 4 , backlog ) ;
1251
+ listen ( self , null , port , 4 , backlog ) ;
1244
1252
1245
1253
} else {
1246
1254
// The first argument is the port, the second an IP.
1247
1255
require ( 'dns' ) . lookup ( arguments [ 1 ] , function ( err , ip , addressType ) {
1248
1256
if ( err ) {
1249
1257
self . emit ( 'error' , err ) ;
1250
1258
} else {
1251
- listen ( self , ip || '0.0.0.0' , port , ip ? addressType : 4 , backlog ) ;
1259
+ listen ( self , ip , port , ip ? addressType : 4 , backlog ) ;
1252
1260
}
1253
1261
} ) ;
1254
1262
}
0 commit comments