Search code examples
javascriptnode.jsmongodbserverside-javascript

How to fetch a site name from URL in Javascript?


Is there an easy way to fetch a site name from a URL string?

Example:

http://www.mysite.com/mypath/mypage -> www.mysite.com
http://mysite.com/mypath/mypage     -> mysite.com

The JS code is executed on mongodb CLI side, not in a browser.


Solution

  • This one worked well for me:

    urlString.split('/')[2]