简体   繁体   English

如何在Node.js中解码URL?

[英]How to decode a URL in nodejs?

I am trying to decode a URL and also format it with URL module in nodejs . 我正在尝试解码URL并使用nodejs URL模块格式化它。

const url = require('url');

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

Here is the returned value for newUrl 这是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 
}

When I finally format it like this: 当我最终将其格式化为:

const formattedUrl = newUrl.format();

It returned: 它返回:

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

But the expected result is: 但是预期结果是:

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

How to handle this situation so it returns the correctly decoded URL? 如何处理这种情况,使其返回正确解码的URL?

尝试这个

decodeURIComponent(newUrl);

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM