Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8961291
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T15:51:22+00:00 2026-06-15T15:51:22+00:00

hi i am trying to short my urls function shorutrl(link) { var linkpost =

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T15:51:23+00:00Added an answer on June 15, 2026 at 3:51 pm

    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.:

    var https = require('https');
    var events = require('events');
    
    function GoogleShortener() {
        events.EventEmitter.call(this);
    };
    
    GoogleShortener.super_ = events.EventEmitter;
    GoogleShortener.prototype = Object.create(events.EventEmitter.prototype, {
        constructor: {
            value: GoogleShortener,
            enumerable: false
        }
    });
    
    GoogleShortener.prototype.shorten = function(link) {
    
        var self = this;
        var data = JSON.stringify({longUrl: link});
    
        var options = {
            hostname: 'www.googleapis.com',
            port: 443,
            path: '/urlshortener/v1/url',
            method: 'POST',
            headers: {
                'Content-Type': 'application/json',
                'Content-Length': data.length
            }
        };
    
        var resp = '';
    
        var req = https.request(options, function(res) {
            res.setEncoding('utf8');
            res.on('data', function(d) {
                resp += d;
            }).on('end', function() {
                try {
                    var id = JSON.parse(resp).id;
                    self.emit('success', id);
                } catch (e) {
                    self.emit('error', e);
                }
            });
        }).on('error', function(e) {
            self.emit('error', e);
        });
    
        req.write(data);
        req.end();
    
        return self;
    };
    
    exports.shorten = function(link) {
        return new GoogleShortener().shorten(link);
    };
    

    usage:

    var g = require('gshort');
    g.shorten('http://www.google.com/').on('success', function(link) {
         console.log(link)
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to write a short function that will let me quickly read in
i am trying to make short url or even pretty URLs inside my wordpress.
In short: Trying to write a wcf service for a winform-app that invokes a
In short I'm trying to browse a Mac's web site on the local wifi
Short version: I'm trying to determine the best way to track what the user
Im trying to add some short file names to a Wise installer to fix
Short question: I'm trying to make that in a given page (uses tabs) back
2 short questions based on trying to make my code more efficient (I think
I am trying to use a malloc of short, something like typedef union _SOME_STRUCT_
I'm trying to write a short Wordpress JQuery for Wordpress comments that would allow

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.