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 135 views
1 answer 142 views
1 answer 184 views
1 answer 250 views
2 answers 1,151 views
1 answer 154 views
1 answer 8,709 views
8,709 views asked Mar 22, 2020 by avibootz
...