Skip to content

Instantly share code, notes, and snippets.

@ArvinH
Forked from schamane/curl.js
Created January 20, 2019 05:09
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 ArvinH/e055ff9d049b44524957e724983bbe96 to your computer and use it in GitHub Desktop.
Save ArvinH/e055ff9d049b44524957e724983bbe96 to your computer and use it in GitHub Desktop.
Curl vs http module for nodejs
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]);
});
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