Answer the question
In order to leave comments, you need to log in
How to properly assemble URL in Node.JS?
How to correctly assemble the URL for a GET request in Nodejs if it has variable components in the middle:
url.com/api/{direction}/{depending on the direction there will be to or from}/rest/json/apiKey=111&appId=111 Yes some kind of module in npm or can it be manually assembled from strings?
thank.
Answer the question
In order to leave comments, you need to log in
Just put a dollar before the brackets:
`url.com/api/${direction}/${direction === 'to'? to: from}/rest/...`
I use locutus.io/php/strings/strtr
module
let direction = 'from';
let url = strtr('url.com/api/{direction}/rest', {
'{direction}' => direction
});
console.log('test url replace placeholder',url);
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question