node.js Firebase 云函数:“错误:无法处理请求”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/45600367/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Cloud Functions for Firebase: 'Error: could not handle the request'
提问by JamesG
I feel like pulling my hair out; this is either super simple and i'm having brain freeze or it is not that simple.
我想拔头发;这要么超级简单,我脑子都冻僵了,要么没那么简单。
What I want
我想要的是
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
我正在尝试使用 firebase 取消缩短缩短的 URL,当用户访问时:myapp.firebaseappurl.com/url/SHORTENEDLINK
SO不会让我添加缩短的 URL
I would like the output to be:
我希望输出是:
{
"url": "https://stackoverflow.com/questions/45420989/sphinx-search-how-to-use-an-empty-before-match-and-after-match"
}
What I have tried
我试过的
firebase.jsonfile:
firebase.json文件:
{
"hosting": {
"public": "public",
"rewrites": [ {
"source": "/url/:item",
"destination": "/url/:item"
} ]
}
}
index.jsfile:
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
结果
When I go to myapp.firebaseappurl.com/url/SHORTENEDLINKI get the following:
当我去时,myapp.firebaseappurl.com/url/SHORTENEDLINK我得到以下信息:
Error: could not handle the request
回答by Manas Jayanth
You are seeing Error: could not handle the requestsince there probably was an exception and it timed out.
您正在看到,Error: could not handle the request因为可能存在异常并且超时。
Check your logs using:
使用以下命令检查您的日志:
firebase functions:log
Refer docsfor more details
有关更多详细信息,请参阅文档
Here's how I got URL unshortening to work
这是我如何使 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 });
}
});
});
You can follow the hello world example straight away and use the above code as your function.
您可以立即遵循 hello world 示例并将上述代码用作您的function.
Above code uses HEADrequests to peek into 'Location` field of the headers and decides if the url can be further unshortened.
上面的代码使用HEAD请求来查看标头的“位置”字段,并决定是否可以进一步缩短 url。
This is lighter as HEAD requests ask for no body (thereby avoiding body parsing). Also, no third party lib required!
这更轻,因为 HEAD 请求不要求正文(从而避免正文解析)。此外,不需要第三方库!
Also note that the url passed as a query param. So the request would be
另请注意,url 作为查询参数传递。所以请求将是
http://<your_firebase_server>/url?url=<short_url>
Saves you the trouble of URL re-writes. Plus semantically makes a little more sense.
免去你重写URL的麻烦。加上语义上更有意义。
回答by bennygenel
Did you tried using { source: '/url/**' }syntax?
你试过使用{ source: '/url/**' }语法吗?
You can use something like this;
你可以使用这样的东西;
{
"hosting": {
"public": "public",
"rewrites": [ {
"source": "/url/**",
"function": "/url"
}]
}
}
and then you can parse the url from the request.
然后您可以从请求中解析 url。
exports.url = functions.https.onRequest((req, res) => {
// parse the url from the req and redirect to the correct link
});
回答by David Debreceni
You should try this in the firebase.json, its worked for me:
你应该在 firebase.json 中试试这个,它对我有用:
"source": "/**",
I also tried "source": "/url/**"but its not worked.
我也尝试过,"source": "/url/**"但没有奏效。
回答by Chris Gunawardena
First make sure you are receiving the request properly with the shortened 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);
Now when you visit myapp.firebaseappurl.com/url/SHORTENEDLINK you should see the SHORTENEDLINK in plain text. When that's working, try the redirect.
现在,当您访问 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);
Also it's good practice to npm installwith --saveso they end up in the packages.json. While firebase copies your node_modules folder, most other SaaS platforms run npm install.
npm install使用 with也是一种很好的做法,--save因此它们最终会出现在packages.json. 虽然 firebase 会复制您的 node_modules 文件夹,但大多数其他 SaaS 平台运行npm install.
回答by Rohan Stark
I think your code is fine. What you're doing incorrectly is that you're using Express-js notations in your firebase.json's rewritesnode. (the :itempart). These don't work in the Firebase Realtime Database.
我认为你的代码很好。您做错的是您在firebase.json'srewrites节点中使用了 Express-js 符号。(:item部分)。这些在 Firebase 实时数据库中不起作用。
So, instead of doing that, change your firebase.jsonto the following :-
因此,不要这样做,而是将您更改firebase.json为以下内容:-
{
"hosting": {
"public": "public",
"rewrites": {
"source": "YOUR SHORTENED URL",
"destination": "YOUR ORIGINAL URL"
}
}
}
This is also the advocated approach in the Cloud Functions for Firebase's URL Shortener sample.
这也是Cloud Functions for Firebase的 URL Shortener示例中提倡的方法。

