Anthony Anthony - 5 months ago 16
HTML Question

How to keep the same path for development and production

In development mode I access a page in my application as:

http://localhost:8080/myapp/color/edit
The same page is accessed in production as
http://www.myapp.com/color/edi


In one of my elements I use the following to POST to the server:

data-url="/red/edit"


When in development, the above turns to
http://localhost:8080/red/edit
and in production it turns to
http://www.myapp.com/red/edit
. So it would work in production but not locally.

Question

How should I put the POST url in
data-url
so that the POST url becomes
http://localhost:8080/myapp/color/edit
in development and
http://www.myapp.com/color/edit
in production

Answer

Create a baseUrl variable and toggle it based on your environment. You can check the window.location value to see which environment your app is being accessed in.

Production:

var baseUrl = "";

Development:

var baseUrl = "/myapp";

Then, define your data-url property using the baseUrl

var dataUrl = baseUrl + "/red/edit";