如何在Node.js中解码URL?

时间:2019-05-01 14:41:44

标签: javascript node.js url encoding decode

我正在尝试解码URL,并使用URL中的nodejs模块对其进行格式化。

const url = require('url');

const oldUrl = "https://tut.by/ad=%24%7Baccount.domain%7D";
const newUrl = url.parse(oldUrl, true).format();

这是newUrl的返回值

{ 
   auth: null
   hash: null
   host: "tut.by"
   hostname: "tut.by"
   href: "https://tut.by/?ad=%24%7Baccount.domain%7D"
   path: "/?ad=%24%7Baccount.domain%7D"
   pathname: "/"
   port: null
   protocol: "https:"
   query: {ad: "${account.domain}"}
   search: "?ad=%24%7Baccount.domain%7D"
   slashes: true 
}

当我最终格式化它时:

const formattedUrl = newUrl.format();

返回:

https://tut.by/?ad=%24%7Baccount.domain%7D

但是预期结果是:

https://tut.by/?ad=${account.domain}

如何处理这种情况,以便它返回正确解码的URL?

1 个答案:

答案 0 :(得分:4)

尝试一下

decodeURIComponent(newUrl);

console.log(decodeURIComponent('https://tut.by/?ad=%24%7Baccount.domain%7D'))

相关问题