Wrap external http request in node js
Wrap external http request in node js
var http = require('http');
var url = require('url').URL;
var shimmer = require('shimmer')
var api = {}
api.wrapEmitter = require('emitter-listener')
var Origional = http.get;
shimmer.wrap(http, 'get', function getWrap(get) {
return makeRequestTrace(get);
})
function makeRequestTrace(request) {
return function trace(options, callback) {
var req = request.call(this, options, function (res) {
console.log('Response===================' , res)
shimmer.wrap(res, 'on', function onWrap(on) {
return function on_trace(eventName, cb) {
if (eventName === 'data') {
on.call(this, 'data', function (chunk) {
console.log('chunk......................')
});
}
return on.apply(this, arguments);
};
});
res.on('end', function () {
console.log('Request end..............................')
});
if (callback) {
return callback(res);
}
});
return req;
};
}
var page = 'http://192.168.8.221:7077/final/#!'
var options = new url(page)
var request = http.get(options, function (res) {
var data = '';
//Read the data chunk by chunk
res.on('data', function (chunk) {
console.log('response data...')
data += chunk;
})
//At the end of the request
res.on('end', function () {
console.log("Page download completed successfully................")
// console.log('success', data)
})
//Error will throw if url not found..etc
res.on('error', function (err) {
console.log("page error", err)
console.log('error', err)
})
})
//To handle the Address not found exception
request.on('error', function (err) {
console.log('Request error ==>', err);
callback('error', err)
});
request.end();
var http = require('http');
var url = require('url').URL;
var shimmer = require('shimmer')
var api = {}
api.wrapEmitter = require('emitter-listener')
var Origional = http.get;
shimmer.wrap(http, 'get', function getWrap(get) {
return makeRequestTrace(get);
})
function makeRequestTrace(request) {
return function trace(options, callback) {
var req = request.call(this, options, function (res) {
console.log('Response===================' , res)
shimmer.wrap(res, 'on', function onWrap(on) {
return function on_trace(eventName, cb) {
if (eventName === 'data') {
on.call(this, 'data', function (chunk) {
console.log('chunk......................')
});
}
return on.apply(this, arguments);
};
});
res.on('end', function () {
console.log('Request end..............................')
});
if (callback) {
return callback(res);
}
});
return req;
};
}
var page = 'http://192.168.8.221:7077/final/#!'
var options = new url(page)
var request = http.get(options, function (res) {
var data = '';
//Read the data chunk by chunk
res.on('data', function (chunk) {
console.log('response data...')
data += chunk;
})
//At the end of the request
res.on('end', function () {
console.log("Page download completed successfully................")
// console.log('success', data)
})
//Error will throw if url not found..etc
res.on('error', function (err) {
console.log("page error", err)
console.log('error', err)
})
})
//To handle the Address not found exception
request.on('error', function (err) {
console.log('Request error ==>', err);
callback('error', err)
});
request.end();
Comments
Post a Comment