Webpack dev proxy in Vue

I am trying to proxy all api/requests to http://localhost:3000using vue-axios vuexand vuex. The output on the command line indicates that the proxy was created, but then it is not really a proxy to the correct address and 404.

I have the following settings inside the web package:

dev: {
  env: require('./dev.env'),
  port: 8080,
  autoOpenBrowser: true,
  assetsSubDirectory: 'static',
  assetsPublicPath: '/',
  proxyTable: {
    'api/': {
      target: 'https://localhost:3000/api',
      changeOrigin: true,
      pathRewrite: {
        '^/api':""
      }
    }
  }
}

And inside my action file, I have:

import Vue from 'vue'

export const register = ({ commit }, user) => {
  return new Promise((resolve, reject) => {
    Vue.axios.post('users', user)
        .then(res => {
          console.log(res)
          debugger
        })
        .catch(err => {
          console.error(err)
          debugger
        })
  })
}

The console output suggests that the proxy has been installed:

[HPM] Proxy created: /api  ->  https://localhost:3000/api
[HPM] Proxy rewrite rule created: "^/api" ~> ""

But when I actually call the function, it returns http://localhost:8080/users 404 (Not Found)

What's wrong with that?

I consulted

.

, HMR, .

?

:

  '/api': {
    target: 'https://localhost:3000',
    secure: false,
    changeOrigin: true
  },

  'api/': {
    target: 'https://localhost:3000',
    secure: false,
    changeOrigin: true
  },

  'api/*': {
    target: 'https://localhost:3000',
    secure: false,
    changeOrigin: true
  },

  '*/api/**': {
    target: 'https://localhost:3000',
    secure: false,
    changeOrigin: true
  },

  '*': {
    target: 'https://localhost:3000',
    secure: false,
    changeOrigin: true
  },

  '/api/*': {
    target: 'http://localhost:3000',
    changeOrigin: true
  }

proxy: {
  "/api": {
    "target": {
      "host": "localhost",
      "protocol": 'http:',
      "port": 3000
    },
    ignorePath: true,
    changeOrigin: true,
    secure: false
  }
},
+6
2

. , - /api . , :

'/api/*': {
    target: 'http://localhost:3000',
    changeOrigin: true
}

:

http://localhost:3000/api

Non-. , , :

pathRewrite: {'^/api' : ''}

:

'/api/*': {
    target: 'http://localhost:3000',
    changeOrigin: true,
    pathRewrite: {'^/api' : ''}
}

, pathRewrite , 1 .

+1

Vue.axios.post("api/users", user).

0

Source: https://habr.com/ru/post/1674929/


All Articles