2011-04-20 19 views

cevap

16

http.get

var options = { 
    host: 'www.google.com', 
    port: 80, 
    path: '/index.html' 
}; 

http.get(options, function(res) { 
    if (res.statusCode == 200) { 
    console.log("success"); 
    } 
}).on('error', function(e) { 
    console.log("Got error: " + e.message); 
}); 

function testPort(port, host, cb) { 
    http.get({ 
    host: host, 
    port: port 
    }, function(res) { 
    cb("success", res); 
    }).on("error", function(e) { 
    cb("failure", e); 
    }); 
} 

bir tcp soket için sadece kullanmak net.createConnection

+0

Çözümünüz, bir HTTP sunucusunu araştırmak için iyidir, ancak belirli bir ana bilgisayarla bir TCP bağlantısı kurabilir miyim diye doğrulamam gerekiyor. – George

+0

@George daha sonra bir tcp bağlantı noktası açın. Yanıt istemek için cevabı – Raynos

+0

10x düzenleyeceğim. Tam olarak ihtiyacım olan şey buydu - tcp bağlantısı kurmak – George

-2

sadece ilgilendiğiniz HTTP ise, (POST/GET yerine), HTTP HEAD isteği kullanabilirsiniz:

function hostAvailable(url) { 
    var req = new XMLHttpRequest(); 
    req.open('HEAD', url, false); 
    req.send(); 
    return req.status!=404; 
} 
+3

Bu node.js kodu geçerli değil. – Raynos

İlgili konular