javascriptvue.js

How to access to domain name inside vue js application?


I have a fullstack MEVN project which I am trying to host. One problem that I am facing is that when I was running the project locally I had hard coded the url to the node server inside my vue js code to make server requests. But now since I have merged the front and back end together for web hosting, my frontend needs to access the Base url(domain name) of the server it is being hosted in so that It can make api request to the same domain name. I could hard code the domain name in vue project before deploying it but there must be a way to look into the domain name inside the application itself. How may I do so?


Solution

  • You may use window.location built-in object, particularly window.location.origin or maybe window.location.host looks particularly similar to what you are looking for.