-
-
Save ArvinH/e055ff9d049b44524957e724983bbe96 to your computer and use it in GitHub Desktop.
Curl vs http module for nodejs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var exec = require('child_process').exec, | |
url = "http://google.com/", | |
timeout = "3", | |
data="?q=test"; | |
var time = process.hrtime(); | |
exec('curl --max-time ' + timeout + ' -d \'' + data + '\' ' + url, function (error, stdout, stderr) { | |
var diff = process.hrtime(time); | |
//console.log('stdout: ' + stdout); | |
//console.log('stderr: ' + stderr); | |
if (error !== null) { | |
console.log('exec error: ' + error); | |
} | |
console.log("process took %d nanoseconds", diff[0] * 1e9 + diff[1]); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var http = require('http'), | |
data="?q=test", | |
options = { | |
hostname: 'www.google.com', | |
port: 80, | |
path: '/', | |
method: 'POST' | |
}, | |
req, time; | |
req = http.request(options, function (res) { | |
res.setEncoding('utf8'); | |
res.on('data', function(chunk) { | |
//console.log(chunk); | |
}); | |
res.on('end', function() { | |
var diff = process.hrtime(time); | |
console.log("process took %d nanoseconds", diff[0] * 1e9 + diff[1]); | |
}); | |
res.on('error', function(error) { | |
console.log('error: ' + error); | |
}); | |
}); | |
time = process.hrtime(); | |
req.write(data); | |
req.end(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment