How to check if a URL is localhost in Node.js

1 Answer

0 votes
function isLocalhost(url) {
  	return url.includes('localhost') || url.includes('127.0.0.1');
}

console.log(isLocalhost('http://localhost:8000/'));

console.log(isLocalhost('http://127.0.0.1:1234/test/'));

console.log(isLocalhost('http://shortinfos.com'));




  
/*
run:
  
true
true
false
  
*/

 



answered Apr 28, 2022 by avibootz

Related questions

1 answer 221 views
1 answer 230 views
1 answer 283 views
1 answer 364 views
2 answers 1,316 views
1 answer 256 views
1 answer 8,810 views
8,810 views asked Mar 22, 2020 by avibootz
...