<p>I have followed a tutorial using Vue as front end and using spring boot as backend. I write the front end and put the built files from Vue's dist folder to spring boot web's <code>src\main\resources\static</code> folder. </p>

<p>The tutorial told me to use axios to transfer data to the backend. It's configuration is as follows:</p>

<pre><code>var axios = require('axios')
axios.defaults.baseURL = 'http://localhost:8090/api'
</code></pre>

<p>It works good on my local pc. But I want to put it and make it run on cloud.
I build the jar with <code>mvn clean install</code>. Then uploaded it to IBM's cloud foundry.
The frontend works. However, it does not talk to the backend. The browser's console log shows:</p>

<pre><code>XHR failed loading: OPTIONS "&lt;URL&gt;".
4xhr.js:178 OPTIONS http://localhost:8090/api/login net::ERR_CONNECTION_REFUSED
</code></pre>

<p>The demo is uploaded <a href="https://github.com/clouddemo1/vuemduidemo1" rel="nofollow noreferrer">here</a> </p>

<p>I also want to include H2 database, but I tried and it only works on mem mode or file mode. The <code>jdbc:h2:tcp://localhost/~/test</code> mode does not work on the cloud.
So how to make it run on the cloud? Is there anyother way to make Vue talk with the java backend without axios?
Or if it's must, can I configure cloud foundry to make the link work?
Or if cloud foundry can't do this, (I use cloud foundry just because it's easy, just upload the jar, no need to configure) can k8s do this?</p>

Haz Wof
<blockquote>
  <p>XHR failed loading: OPTIONS "".
  4xhr.js:178 OPTIONS <a href="http://localhost:8090/api/login" rel="nofollow noreferrer">http://localhost:8090/api/login</a> net::ERR_CONNECTION_REFUSED</p>
</blockquote>

<p>This is failing because your app is no longer running on your local machine, it's running on Cloud Foundry. You need to update your <code>axios.defaults.baseURL</code> setting to reference the route that you bound to your app.</p>

<p>You could hard code this in the config, which is not great but does work, or you could reference the <code>VCAP_APPLICATION</code> environment variable which is set by Cloud Foundry and contains information about your app, including the bound routes (there can be more than one). You could read this, pick a route and dynamically configure your app.</p>

<p>Ex:</p>

<pre><code> "VCAP_APPLICATION": {
  "application_id": "&lt;guid&gt;",
  "application_name": "&lt;app-name&gt;",
  "application_uris": [
   "app-name.apps.example.com",
   "some-other-route.example.com"
  ],
  "application_version": "df82308c-7add-4f2b-bb44-a58680084a79",
  "cf_api": "https://api.system.example.com",
  "limits": {
   "disk": 1024,
   "fds": 16384,
   "mem": 64
  },
  "name": "&lt;app-name&gt;",
  "space_id": "&lt;space-guid&gt;",
  "space_name": "&lt;space-name&gt;",
  "uris": [
   "app-name.apps.example.com",
   "some-other-route.example.com"
  ],
  "users": null,
  "version": "df82308c-7add-4f2b-bb44-a58680084a79"
 }
</code></pre>

<blockquote>
  <p>I also want to include H2 database, but I tried and it only works on mem mode or file mode. The <code>jdbc:h2:tcp://localhost/~/test</code> mode does not work on the cloud.</p>
</blockquote>

<p>I don't see why this technically wouldn't work, so long as you're app is talking to the database over localhost, all the traffic would be inside the app container. You'd have to be more specific about what exactly is failing.</p>

<p>That said, I don't think you'd want to use H2 like that, at least not beyond a small test/demo. First off, the app containers are ephemeral, so none of your data would survive a restart/crash/restage/push or anything that triggers the container to restart (platform maintenance can do this too, so it's not just actions you initiate). Second, you really wouldn't be able to scale your app beyond one instance because each instance of the app would have it's own copy of the database, which is going to be problematic.</p>

<p>What you really want to do is create a service &amp; bind that to your app. Then, just like <code>VCAP_APPLICTION</code>, you can pull your service credentials from <code>VCAP_SERVICES</code> and dynamically configure your app to connect to your database. See the second part of this <a href="https://stackoverflow.com/a/56257656/1585136">answer</a> for details on how to do that. </p>

<p>Try running <code>cf marketplace</code> to see a list of the services your provider offers. Many services even have free tier service plans, so you can try them or use them for small apps and demos.</p>

<p>Hope that helps!</p>
