Firebase 的云功能:“错误:无法处理请求" [英] Cloud Functions for Firebase: 'Error: could not handle the request'
问题描述
我想拔头发;这要么超级简单,我的大脑冻结了,要么没那么简单.
I feel like pulling my hair out; this is either super simple and i'm having brain freeze or it is not that simple.
当用户转到以下位置时,我正在尝试使用 Firebase 缩短缩短的 URL:myapp.firebaseappurl.com/url/SHORTENEDLINK
所以不会让我添加缩短的 URL
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
我希望输出是:
{
"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
时,我得到以下信息:
Result
When I go to myapp.firebaseappurl.com/url/SHORTENEDLINK
I get the following:
Error: could not handle the request
推荐答案
你看到 Error: could not handle the request
因为可能有异常并且超时.
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
请参阅 docs 了解更多详情
Refer docs for more details
这就是我如何让 URL 缩短工作
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 });
}
});
});
您可以直接按照 hello world 示例,将上面的代码用作您的 函数
.
You can follow the hello world example straight away and use the above code as your function
.
上面的代码使用 HEAD
请求来查看标头的Location"字段并决定是否可以进一步缩短 url.
Above code uses HEAD
requests to peek into 'Location` field of the headers and decides if the url can be further unshortened.
这更轻松,因为 HEAD 请求不要求正文(从而避免正文解析).此外,不需要第三方库!
This is lighter as HEAD requests ask for no body (thereby avoiding body parsing). Also, no third party lib required!
另请注意,url 作为查询参数传递.所以请求将是
Also note that the url passed as a query param. So the request would be
http://<your_firebase_server>/url?url=<short_url>
省去了重写 URL 的麻烦.Plus 在语义上更有意义.
Saves you the trouble of URL re-writes. Plus semantically makes a little more sense.
这篇关于Firebase 的云功能:“错误:无法处理请求"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!