Home > OS >  separate GET request path from query variables
separate GET request path from query variables

Time:06-23

I have the url: http://www.localhost:8080/api/numbers/prime?amount=13 via GET request

I want to get api/numbers/prime is there any way to do that in node.js vanilla?

CodePudding user response:

You can use JavaScript split and slice methods on Strings.

let url = "http://www.localhost:8080/api/numbers/prime?amount=13";
let urlWithoutQuery = url.split("?")[0];
let path = urlWithoutQuery.slice(25); // To get the value after http://www.localhost:8080

CodePudding user response:

You can use URL:

const url = new URL('http://www.localhost:8080/api/numbers/prime?amount=13')

then:

console.log(url.pathname); 
// outputs: 
// /api/numbers/prime

console.log(url.searchParams); 
// outputs:
// URLSearchParams { 'amount' => '13' }

console.log(url.search);
// outputs:
// ?amount=13
  • Related