Firebase云端函数:“错误:无法处理请求” [英] Cloud Functions for Firebase: 'Error: could not handle the request'
问题描述
我想要什么
我试图用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
file:
<$ p $
hosting:{
public:public,
rewrites:[{
source:/ url /:item,
destination:/ url /:item
}]
}
}
pre>
index.js
file:
const函数= require('firebase-functions');
$ b $ 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);
}
);
});
结果
code> myapp.firebaseappurl.com/url/SHORTENEDLINK 我得到以下内容:
错误:无法处理请求
{source:'/ url / **'}
语法
你可以使用类似的东西; p>
{
hosting:{
public:public,
rewrites :[{
source:/ url / **,
function:/ url
}]
}
}
然后您可以从请求中解析网址。
exports.url = functions.https.onRequest((req,res)=> {
//从req解析url并重定向到正确的链接
});
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
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.json
file:
{
"hosting": {
"public": "public",
"rewrites": [ {
"source": "/url/:item",
"destination": "/url/:item"
} ]
}
}
index.js
file:
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/SHORTENEDLINK
I get the following:
Error: could not handle the request
Did you tried using { source: '/url/**' }
syntax?
You can use something like this;
{
"hosting": {
"public": "public",
"rewrites": [ {
"source": "/url/**",
"function": "/url"
}]
}
}
and then you can parse the url from the request.
exports.url = functions.https.onRequest((req, res) => {
// parse the url from the req and redirect to the correct link
});
这篇关于Firebase云端函数:“错误:无法处理请求”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!