简体   繁体   中英

Javascript: Is there a way to automatic change localhost to the ip address when go live

I have a website which is completely client-side. When it is in a testing environment, I use localhost for all links to other services: databases, report builders, etc.

When the website is deployed to apache2, for to go live, I have to manually change those link to the IP address of the server.

Is there any way in code to avoid doing this? Or, is the only way to automatically change them (grunt for example) during the deployment phase?

You could check the window.location object and put some conditional logic around the hostname, eg if (window.location.hostname == 'localhost') {} .

Although mixing environment specific / test-time code is typically a smell. Your hunch about this being a build/deploy time activity is probably the right way to go. The gulp-inject plugin is worth considering.

how I'd do it. (nginx)

  1. run my application from server (you can access it via localhost on the server)
  2. use nginx to serve the localhost application whenever a request comes to the server.

you can do the same with apache2:

i was about to write how to do it, but this website says it all: http://subinsb.com/linux-apache-localhost

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM