2017-10-07 20 views
0

我試圖連接到遠程服務器,但每次嘗試創建連接時,我都會收到 套接字錯誤「code」:「ECONNREFUSED」 「錯誤號」: 「ECONNREFUSED」, 「系統調用」: 「連接」, 「地址」: 「127.0.0.1」, 「口」: 「80」} 我的代碼如下:嘗試使用nodejs訪問遠程服務器時出現套接字錯誤「code」:「ECONNREFUSED」

//create the connection 
const client = net.createConnection({ 
    host: urlObj.host, 
    port: urlObj.port 
}); 


//on receiving the data, print it 
client.on('data', function (data) { 
    if (verbose) { 
     console.log(data.toString()); 
    } else { 
     //get rid of the head of the response if not verbose mode 
     var responseSplit = data.toString().split("\r\n"); 
     var inBody = false; 
     for (var i in responseSplit) { 
      if (responseSplit[i] == "") { 
       inBody = true; 
      } 
      if (inBody) { 
       console.log(responseSplit[i]); 
      } 
     } 
    } 
}); 

//send the request 
client.on('connect',() => { 
    client.write(clientWritingString); 
}); 

client.on('error', err => { 
    console.log('socket error %j', err); 
    process.exit(0); 
}); 

而且我在這裏解析Url:

//parse the URL 

var urlToParse = process.argv[process.argv.length - 1]; 
var urlObj = url.parse(urlToParse); 
if(urlObj.port==null){ 
    urlObj.port=80; 
} else { 
    urlObj.host = urlObj.host.split(":")[0]; 
} 

回答

0

錯誤消息顯示您的程序試圖訪問您自己的計算機(= 127.0.0.1)。檢查您正在提供給例程的原始URL,並確保解析器以實際的遠程URL結束。

+0

是的,我覺得我一切正常,但它仍然嘗試訪問本地主機。我能做什麼? – BleachedAxe

相關問題