0

我正在尝试连接到远程服务器,但每次尝试创建连接时,我都会收到套接字错误“code”:“ECONNREFUSED”,“errno”:“ECONNREFUSED”,“syscall”:“connect”,“address”: "127.0.0.1","port":"80"} 我的代码如下:

//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);
});

我在这里解析网址:

//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];
}
4

1 回答 1

0

错误消息显示您的程序正在尝试访问您自己的机器 (= 127.0.0.1)。检查您提供给例程的原始 URL,并确保解析器以实际的远程 URL 结束。

于 2017-10-07T20:38:48.977 回答