S
S
Sergey Nekrasov2017-09-19 16:30:14
JavaScript
Sergey Nekrasov, 2017-09-19 16:30:14

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

2 answer(s)
V
Vitaly, 2017-09-19
@chevylevel

Just put a dollar before the brackets:
`url.com/api/${direction}/${direction === 'to'? to: from}/rest/...`

A
Alexander N++, 2017-09-19
@sanchezzzhak

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);

interestingly done, you can connect the necessary files as written in the documentation

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question