Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vue proxy setting does not work

I have a Vue project from @vue/cli 3.x.

The proxy I defined in package.json based on this article is not working. The destination server doesn't see the API request.

What am I missing here?

The vue file:

<script lang="ts">
import { Component, Prop, Vue } from 'vue-property-decorator';
import VueResource from 'vue-resource';

Vue.use(VueResource);

@Component
export default class HelloWorld extends Vue {
  @Prop() private msg!: string;

  constructor() {
    super();

    this.$http.post('/api');
  }
}
</script>

package.json:

  "proxy": {
    "/api": "http://localhost:9000/api"
  },
like image 314
raxinaga Avatar asked Jun 18 '18 15:06

raxinaga


1 Answers

The article likely refers to an outdated method of setting up the proxy. The latest version of @vue/cli (currently 3.0.0-rc.3) has a new method of configuring the dev server.

For an equivalent setup of that proxy, create vue.config.js (if it doesn't exist already) with the following contents:

module.exports = {
  devServer: {
    proxy: {
      '/api': {
        target: 'http://localhost:9000',
        ws: true,
        changeOrigin: true
      }
    }
  }
}
like image 81
tony19 Avatar answered Nov 19 '22 04:11

tony19