hi i am trying to short my urls
function shorutrl(link)
{
var linkpost = JSON.stringify({longUrl:link});.
var optionslink = {
host: "www.googleapis.com",
port: 443,
method: 'POST',
path: "/urlshortener/v1/url",
headers: {
'Content-Type' : 'application/json'
}
};
optionslink.headers['Content-Length']=linkpost.length;
var linkreq = https.request(optionsimg, function(res) {
res.on('data', function (d) {
linkdata+=d;
});
res.on('end', function (x) {
try { return JSON.parse(linkdata).id; } catch(e){ return link; }
});
}).on('error', function (e) {
//console.log(e.message);
});
linkreq.write(optionslink);
linkreq.end();
}
function nonworking_givelink()
{
return shorutrl(txtlinks[Math.floor(Math.random() * txtlinks.length)]);
}
function working_givelink()
{
return txtlinks[Math.floor(Math.random() * txtlinks.length)];
}
nonworking_givelink returns undefined working_givelink returns link as normal
should i write a new function and pass paramters to that and generate new link and pass the paramters to another function is there no any easier way?
You shouldn’t write blocking code in node.js, it goes against the very design of the system.
You need to pass a callback function which will be called with the new URL within your
.on('end', ...)block.Alternatively, wrap this up into a new object which itself emits events, e.g.:
usage: