Firebase的云功能:'错误:无法处理请求'

Jam*_*esG 15 url-shortener node.js firebase firebase-hosting google-cloud-functions

我想把头发拉出来; 这是超级简单的,我有脑冻结或它不是那么简单.

我想要的是

当用户访问时,我试图使用firebase取消缩短缩短的URL:所以
myapp.firebaseappurl.com/url/SHORTENEDLINK
我不会让我添加缩短的URL

我希望输出为:

{
  "url": "https://stackoverflow.com/questions/45420989/sphinx-search-how-to-use-an-empty-before-match-and-after-match"
}
Run Code Online (Sandbox Code Playgroud)

我试过了什么

firebase.json 文件:

{
  "hosting": {
    "public": "public",
    "rewrites": [ {
    "source": "/url/:item",
      "destination": "/url/:item"
    } ]
  }
}
Run Code Online (Sandbox Code Playgroud)

index.js 文件:

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);
      }
    );

});
Run Code Online (Sandbox Code Playgroud)

结果

当我去的时候,myapp.firebaseappurl.com/url/SHORTENEDLINK我得到以下内容:

Error: could not handle the request
Run Code Online (Sandbox Code Playgroud)

Man*_*nth 6

您正在看到,Error: could not handle the request因为可能有一个异常,并且超时了。

使用以下方法检查日志:

firebase functions:log
Run Code Online (Sandbox Code Playgroud)

有关更多详细信息,请参考文档

这是我使URL缩短工作的方式

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 });
    }
  });
});
Run Code Online (Sandbox Code Playgroud)

您可以立即按照hello world示例进行操作,并使用上述代码作为您的代码function

上面的代码使用HEAD请求来窥视标题的“位置”字段,并确定是否可以进一步缩短该网址。

由于HEAD请求不要求提供任何主体,因此较轻(从而避免了主体解析)。另外,不需要第三方库!

另请注意,该URL作为查询参数传递。所以要求是

http://<your_firebase_server>/url?url=<short_url>
Run Code Online (Sandbox Code Playgroud)

避免了URL重写的麻烦。加上语义上的意义。


Roh*_*ark 0

我认为你的代码很好。您做错的事情是您在 的 节点中使用了 Express-jsfirebase.json符号rewrites。(那个:item部分)。这些在 Firebase 实时数据库中不起作用。

因此,不要这样做,而是将您的更改firebase.json为以下内容:-

 {
  "hosting": {
    "public": "public",
    "rewrites":  {
    "source": "YOUR SHORTENED URL",
    "destination": "YOUR ORIGINAL URL"
  } 
  }
}
Run Code Online (Sandbox Code Playgroud)

这也是Cloud Functions for Firebase的 URL Shortener示例中推荐的方法。