1

I am trying to decode a URL and also format it with URL module in nodejs.

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

{ 
   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?

1 Answer 1

5

Try this

decodeURIComponent(newUrl);

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.