Skip to content

Instantly share code, notes, and snippets.

@rafalw
Created December 15, 2011 21:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rafalw/1483025 to your computer and use it in GitHub Desktop.
Save rafalw/1483025 to your computer and use it in GitHub Desktop.
Modyfikacja kodu z gist 1390021
#!/usr/bin/env node
// Bardzo^3 prosty CURL-like
// Wywołanie:
// node requesturl.js adres_strony_bez_http
// np.:
// node requesturl.js wileczeknet.blogspot.com
// Wersja 2
// - zastąpiono klasyczną obsługę parametrów linii polecenia funkcjonalnością dostarczaną
// przez moduł "optimist" (opis: http://www.catonmat.net/blog/nodejs-modules-optimist/)
// TODO:
// - w miarę możliwości rozbudować program (dodać inne opcje, np. wysyłanie danych formularza itp.)
var http = require("http"),
argv = require("optimist").argv;
if (argv._.length == 1) {
// Prawdopodobnie podano adres strony, można rozpocząć pobieranie
console.log("Rozpoczynam pobieranie strony '" + argv._[0] + "'");
// Port 80 hardcoded ;-)
// A przy okazji: można użyć http.get zamiast http.request (jesli już hardcodujemy)...
var req = http.request({ host: argv._[0], port: 80, path: "/", method: "GET" }, function(response) {
var data = "";
var chunks = 0;
response.setEncoding('utf-8');
response.on("data", function(chunk) {
data += chunk;
chunks++;
});
response.on("end", function() {
console.log(data);
console.log("\nLiczba paczek: " + chunks);
console.log("Liczba odebranych znaków: " + data.length);
console.log("Liczba odebranych bajtów: " + Buffer.byteLength(data, 'utf-8'));
});
});
req.on("error", function(err) {
console.log("Błąd: " + err.message);
});
req.end();
} else {
console.log("Błędna liczba parametrów");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment