0

I have a constant link looking like this:

http://link.com/?val1=val1&val2=val2

And this link redirects me to a new link with a random value of a constant param such like;

http://link2.com/?constant=randomvalue/

Each time I use the first link, I get a random value from the following link.

By using Node.js, how can I catch the 'randomvalue' of 'constant' in the second link?

I have to use the first link to reach the second one.

2 Answers 2

1

Try reading the second link as a URL

let secondURL = new URL("http://link2.com/?constant=randomvalue/");

Then extract the value of the constant searchparam like so

let constantValue = secondURL.searchParams.get("constant"); //"randomvalue/"
Sign up to request clarification or add additional context in comments.

2 Comments

But I have to use the first link to reach the second one, so directly reaching the second link won't make any sense because the random value will stay same.
Then you should probably update your question with an example of what you want to achieve
0

@Misantorp's answer is probably best, but there is another way to do it. Check out the querystring module built into Node, it has a convenient parse method just for things like this: https://nodejs.org/api/querystring.html

This should work:

const querystring = require('querystring');

querystring.parse("http://link2.com/?constant=randomvalue/"); // { 'http://link2.com/?constant': 'randomvalue/' }

You may want to substring from the ? onwards to make it more clear:

const str = "http://link2.com/?constant=randomvalue/";
const paramIndex = str.indexOf("?");
if (paramIndex >= 0) {
    const queryParamStr = str.substr(str.indexOf("?"));
    const queryParams = querystring.parse(queryParamStr);
    console.log(queryParams["constant"]);
}

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.