我想把头发拉出来;这是超级简单的,我有脑冻结或它不是那么简单。
当用户访问时,我试图使用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
有关详细信息,请参阅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重写的麻烦。加上语义更有意义。
你尝试过使用{ 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
});
你应该在firebase.json中尝试这个,它对我有用:
"source": "/**",
我也试过"source": "/url/**"
,但它不起作用。
我认为你的代码很好。您正在做的错误是您在firebase.json
的rewrites
节点中使用Express-js符号。 (:item
部分)。这些在Firebase实时数据库中不起作用。
所以,不要这样做,而是将你的firebase.json
改为: -
{
"hosting": {
"public": "public",
"rewrites": {
"source": "YOUR SHORTENED URL",
"destination": "YOUR ORIGINAL URL"
}
}
}
这也是Firebase的URL Shortener sample的Cloud Functions中提倡的方法。
首先确保您使用缩短的网址正确接收请求。
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
。