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 118 views
1 answer 129 views
1 answer 170 views
1 answer 232 views
2 answers 1,123 views
1 answer 142 views
1 answer 8,697 views
8,697 views asked Mar 22, 2020 by avibootz
...