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

问题描述 投票:12回答:5

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

我想要的是

当用户访问时,我试图使用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"
}

我试过了什么

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

});

Result

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

Error: could not handle the request
node.js firebase google-cloud-functions firebase-hosting url-shortener
5个回答
5
投票

你看到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重写的麻烦。加上语义更有意义。


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

1
投票

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

"source": "/**",

我也试过"source": "/url/**",但它不起作用。


0
投票

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

所以,不要这样做,而是将你的firebase.json改为: -

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

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


0
投票

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

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

最新问题
© www.soinside.com 2019 - 2025. All rights reserved.