Skip to content

Instantly share code, notes, and snippets.

@sspencer
Created April 13, 2011 20:57
Show Gist options
  • Save sspencer/918398 to your computer and use it in GitHub Desktop.
Save sspencer/918398 to your computer and use it in GitHub Desktop.
send multiple requests
var http = require("http");
// Broken multiproxy - req.socket returns undefined for second hostname when
// it's equal to first hostname.
function sendRequest(hostname) {
var options = {
host: hostname,
port: 80,
path: "/"
};
var req = http.request(options, function(res) {
var body = [];
res.on("data", function (chunk) {
body.push(chunk);
});
res.on("end", function() {
console.log("Received " + body.join("").length + " bytes from " + hostname);
});
});
req.socket.setTimeout(5000);
req.socket.on("timeout", function() {
req.socket.destroy();
console.log("request " + hostname + " timed out.");
});
req.end();
}
// Works with 2 different hosts
//var requests = ["www.yahoo.com", "www.google.com"];
// Socket undefined for same host
var requests = ["www.google.com", "www.google.com"];
requests.forEach(function(r) {
sendRequest(r);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment