【问题标题】:Cloud Functions for Firebase: 'Error: could not handle the request'Firebase 的云功能:“错误:无法处理请求”
【发布时间】:2018-01-17 21:58:00
【问题描述】:

我想拔头发;这要么超级简单,我脑子都冻僵了,要么没那么简单。

我想要什么

当用户转到:
myapp.firebaseappurl.com/url/SHORTENEDLINK
SO 不允许我添加缩短的 URL

我希望输出是:

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

我尝试过的

firebase.json文件:

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

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

});

结果

当我转到myapp.firebaseappurl.com/url/SHORTENEDLINK 时,我得到以下信息:

Error: could not handle the request

【问题讨论】:

  • 在请求中添加method : 'POST'
  • 为什么?传递的参数在 URL 中,所以是 GET 请求吧?
  • @mohamadrabee - 另外,只是试了一下,看看是不是这样。没有。没用。
  • 对不起我的错误
  • 你能分享你的 package.json 吗?您是否在依赖项中有“请求”

标签: node.js firebase google-cloud-functions firebase-hosting url-shortener


【解决方案1】:

您看到的是Error: could not handle the request,因为可能存在异常并且已超时。

使用以下方式检查您的日志:

firebase functions:log

更多详情请参考docs

这就是我如何让 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 });
    }
  });
});

您可以直接按照 hello world 示例,使用上面的代码作为您的function

上面的代码使用HEAD 请求来查看标头的“Location”字段并决定是否可以进一步缩短url。

这更轻松,因为 HEAD 请求不要求正文(从而避免正文解析)。此外,不需要第三方库!

另请注意,url 作为查询参数传递。所以请求将是

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

省去了重写 URL 的麻烦。 Plus 在语义上更有意义。

【讨论】:

【解决方案2】:

您是否尝试过使用{ source: '/url/**' } 语法?

你可以使用这样的东西;

{
  "hosting": {
    "public": "public",
    "rewrites": [ {
    "source": "/url/**",
    "function": "/url"
    }]
  }
}

然后你就可以解析请求中的url了。

 exports.url = functions.https.onRequest((req, res) => { 
   // parse the url from the req and redirect to the correct link
 });

【讨论】:

    【解决方案3】:

    你应该在 firebase.json 中试试这个,它对我有用:

    "source": "/**",
    

    我也试过"source": "/url/**",但没用。

    【讨论】:

      【解决方案4】:

      我认为您的代码很好。您做错了什么是您在firebase.jsonrewrites 节点中使用了Express-js 符号。 (:item 部分)。这些在 Firebase 实时数据库中不起作用。

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

       {
        "hosting": {
          "public": "public",
          "rewrites":  {
          "source": "YOUR SHORTENED URL",
          "destination": "YOUR ORIGINAL URL"
        } 
        }
      }
      

      这也是 Cloud Functions for Firebase 的 URL Shortener sample 中提倡的方法。

      【讨论】:

      • 这不是动态的。我希望能够将 url 作为参数传递给函数。有什么想法吗?
      • @Rohan 该示例仅适用于 goo.gl 网址。此外,可以使用比 GET 更轻量级的 HEAD 请求来完成 url 解析
      • @JamesG 是的,它不会是动态的。但是,只要缩短原始 URL,您就可以将其简单地保存在您指定的位置。对于访问原始 URL,您只需在 Cloud 函数文件中添加代码,以便在 HTTP 触发期间从指定位置读取它,并将其用作响应的 URL。这当然不会是动态的。但是,您不必即时取消缩短网址,也不必包含第三方应用程序。
      【解决方案5】:

      首先确保您使用缩短的 url 正确接收请求。

      const functions = require('firebase-functions');
      
      const express = require('express');
      var express_app = express();
      express_app.use(body_parser.text({type: ()=>true}));
      express_app.all('*', (req, res) => {
          console.log(req.path);
          res.send(JSON.stringify(req.path));
      });
      exports.url = functions.https.onRequest(express_app);
      

      现在,当您访问 myapp.firebaseappurl.com/url/SHORTENEDLINK 时,您应该会看到纯文本的 SHORTENEDLINK。当它起作用时,尝试重定向。

      const functions = require('firebase-functions');
      const express = require('express');
      const request = require('request');
      var express_app = express();
      express_app.use(body_parser.text({type: ()=>true}));
      express_app.all('*', (req, res) => {
          var url = req.path;
          request({
              uri: uri,
              followRedirect: true
            },
            function(err, httpResponse) {
              if (err) {
                return console.error(err);
              }
              res.send(httpResponse.headers.location || uri);
            }
          );
      });
      exports.url = functions.https.onRequest(express_app);
      

      npm install--save 也是一个很好的做法,因此它们最终会出现在 packages.json 中。当 firebase 复制您的 node_modules 文件夹时,大多数其他 SaaS 平台运行 npm install

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-07-03
        • 1970-01-01
        • 2018-04-13
        • 2019-01-31
        • 2021-12-17
        • 2019-09-12
        • 2020-10-30
        • 1970-01-01
        相关资源
        最近更新 更多