我正在尝试构建URL缩写。我可以使用npm模块'shortid'成功缩短网址。但是无法将其扩展回去。我已经尝试过'resuest','unshort'和'url-unshort'npm模块。我可以使用哪个其他模块来扩展缩短的网址?
POST:http://localhost:7000/api/item
{ “ originalUrl”:“ https://www.amazon.in/Apple-iPhone-X-Silver-256GB/dp/B071P37652/ref=sr_1_1?s=electronics&ie=UTF8&qid=1522661136&sr=1-1&keywords=iphone”, “ shortBaseUrl”:“ http://localhost” }
返回缩短的网址:http://localhost/HkEb8OAGm
let uu = require('url-unshort')();
//to get the expanded url
app.get("/api/item/:code", async (req, res) => {
const urlCode = req.params.code;
console.log(req.params.code);
const item = await UrlShorten.findOne({ urlCode: urlCode });
if(item){
var shortU = item.shortUrl;
uu.expand(shortU)
.then(url => {
if (url) console.log('Original url is: ${url}');
else console.log('This url can\'t be expanded');
})
.catch(err => console.log(err));
}
});
谢谢。