Firebase云端函数:“错误:无法处理请求”

我想把头发拉出来, 这要么是超级简单,要么是脑冻结,要么不是那么简单。

我想要的是

我正尝试使用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); } ); }); 

结果

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

 Error: could not handle the request 

你看到Error: could not handle the request因为可能有一个例外,它超时。

检查你的日志使用:

 firebase functions:log 

请参阅文档了解更多详情

以下是我如何使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世界的例子,并使用上面的代码作为你的function

上面的代码使用HEAD请求来查看标题的“位置”字段,并决定是否可以进一步取消url。

由于HEAD请求不要求主体(从而避免了主体parsing),所以这个更轻。 另外,不需要第三方库!

另外请注意,作为查询parameter passing的url。 所以请求会是

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

为您节省了URL重写的麻烦。 加上语义上更有意义。

你尝试过使用{ source: '/url/**' }语法吗?

你可以用这样的东西;

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

然后你可以从请求中parsingurl。

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

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

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

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

这也是Firebase URL Shortener 示例的Cloud Functions中的主张方法。

首先确保你正在收到请求与缩短的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时,您应该以纯文本forms查看SHORTENEDLINK。 当这个工作,尝试redirect。

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

另外,最好的做法是使用--savenpm install ,这样就可以在packages.json结束了。 虽然Firebase会复制您的node_modules文件夹,但其他大多数SaaS平台都会运行npm install