Sign Up

Have an account? Sign In Now

Sign In

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

Sorry, you do not have a permission to ask a question, You must login to ask question.

Forgot Password?

Need An Account, Sign Up Here
Sign InSign Up

ErrorCorner

ErrorCorner Logo ErrorCorner Logo

ErrorCorner Navigation

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

Mobile menu

Close
Ask a Question
  • Home
  • Contact Us
  • About Us
Home/ Questions/Q 966
Answered
Kenil Vasani
Kenil Vasani

Kenil Vasani

  • 646 Questions
  • 567 Answers
  • 77 Best Answers
  • 26 Points
View Profile
  • 4
Kenil Vasani
Asked: December 22, 20202020-12-22T07:09:03+00:00 2020-12-22T07:09:03+00:00

Cloud Functions for Firebase: ‘Error: could not handle the request’

  • 4

I feel like pulling my hair out; this is either super simple and i’m having brain freeze or it is not that simple.

What I want

I am trying to unshorten a shortened URL using firebase, when a user goes to:
myapp.firebaseappurl.com/url/SHORTENEDLINK
SO wont let me add a shortened URL

I would like the output to be:

{
  "url": "https://stackoverflow.com/questions/45420989/sphinx-search-how-to-use-an-empty-before-match-and-after-match"
}

What I have tried

firebase.json file:

{
  "hosting": {
    "public": "public",
    "rewrites": [ {
    "source": "/url/:item",
      "destination": "/url/:item"
    } ]
  }
}

index.js file:

const functions = require('firebase-functions');

exports.url = functions.https.onRequest((requested, response) => {

    var uri = requested.url;
    request({
        uri: uri,
        followRedirect: true
      },
      function(err, httpResponse) {
        if (err) {
          return console.error(err);
        }
        response.send(httpResponse.headers.location || uri);
      }
    );

});

Result

When I go to myapp.firebaseappurl.com/url/SHORTENEDLINK I get the following:

Error: could not handle the request
firebasefirebase-hostinggoogle-cloud-functionsnode.jsurl-shortener
  • 1 1 Answer
  • 11 Views
  • 0 Followers
  • 0
Answer
Share
  • Facebook

    1 Answer

    • Voted
    1. Kenil Vasani

      Kenil Vasani

      • 646 Questions
      • 567 Answers
      • 77 Best Answers
      • 26 Points
      View Profile
      Best Answer
      Kenil Vasani
      2020-12-22T07:07:26+00:00Added an answer on December 22, 2020 at 7:07 am

      You are seeing Error: could not handle the request since there probably was an exception and it timed out.

      Check your logs using:

      firebase functions:log
      

      Refer docs for more details

      Here’s how I got URL unshortening to work

      const functions = require('firebase-functions');
      
      const admin = require('firebase-admin');
      admin.initializeApp(functions.config().firebase);
      
      const http = require('http');
      const urlP = require('url');
      
      const unshorten = (url, cb) => {
        const _r = http.request(
          Object.assign(
            {},
            urlP.parse(url),
            {
              method: 'HEAD',
            }
          ),
          function(response) {
            cb(null, response.headers.location || url);
          }
        );
        _r.on('error', cb);
        _r.end();
      };
      
      const resolveShortUrl = (uri, cb) => {
        unshorten(uri, (err, longUrl) => {
          if (longUrl === uri) {
            cb(null, longUrl);
          } else {
            resolveShortUrl(longUrl, cb);
          }
        });
      };
      
      exports.url = functions.https.onRequest((requested, response) => {
        var uri = requested.query.url;
        resolveShortUrl(uri, (err, url) => {
          if (err) {
            // handle err
          } else {
            response.send({ url });
          }
        });
      });
      

      You can follow the hello world example straight away and use the above code as your function.

      Above code uses HEAD requests to peek into ‘Location` field of the headers and decides if the url can be further unshortened.

      This is lighter as HEAD requests ask for no body (thereby avoiding body parsing). Also, no third party lib required!

      Also note that the url passed as a query param. So the request would be

      http://<your_firebase_server>/url?url=<short_url>
      

      Saves you the trouble of URL re-writes. Plus semantically makes a little more sense.

      • 8
      • Share
        Share
        • Share on Facebook
        • Share on Twitter
        • Share on LinkedIn
        • Share on WhatsApp

    You must login to add an answer.

    Forgot Password?

    Sidebar

    Ask A Question
    • Popular
    • Kenil Vasani

      SyntaxError: invalid syntax to repo init in the AOSP code

      • 5 Answers
    • Kenil Vasani

      runtimeError: package fails to pass a sanity check for numpy ...

      • 3 Answers
    • Kenil Vasani

      xlrd.biffh.XLRDError: Excel xlsx file; not supported

      • 3 Answers
    • Kenil Vasani

      Homebrew fails on MacOS Big Sur

      • 3 Answers
    • Kenil Vasani

      Error: PostCSS plugin tailwindcss requires PostCSS 8

      • 2 Answers

    Explore

    • Most Answered
    • Most Visited
    • Most Voted
    • Random

    © 2020-2021 ErrorCorner. All Rights Reserved
    by ErrorCorner.com