javascript - Node Call Export on Callback -
i'm banging head not learning basics , jumping in.
i'm building api returns ssl certificate status of domain. it's working fine on console.log json output empty, because exports executed before https request ends.
how incorporate exports in response.on(end) function? lot!
function getssl(domain) { var options = { host: 'www.'+domain+'.com', method: 'get', path: '/' }; var isauth = false; callback = function(response) { response.on('data', function () { isauth = response.socket.authorized; }); response.on('end', function () { console.log(isauth); }); } var req = https.request(options, callback).end(); } exports.findbydomain = function (req, response) { var id = req.params.id; sslcheck = getssl(id); response.send(sslcheck); };
yes, response.send(sslcheck); gets executed before getssl(id); has chance finish. need send in callback can executed after getssl(id); finishes:
function getssl(domain, callback) { var options = { host: 'www.'+domain+'.com', method: 'get', path: '/' }; var isauth = false; var httpcallback = function(response) { response.on('data', function () { isauth = response.socket.authorized; }); response.on('end', function () { console.log(isauth); callback(isauth); }); } var req = https.request(options, httpcallback).end(); } exports.findbydomain = function (req, response) { var id = req.params.id; getssl(id, function(sslcheck) { response.send(sslcheck); }); };
Comments
Post a Comment