我正在尝试连接到远程服务器,但每次尝试创建连接时,我都会收到套接字错误“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];
}