Marcel
Marcel

Reputation: 428

Why is my vue.config.js proxy not working?

I have a vue cli 3.5.0 project and am trying to add a proxy, but i cant get it to work. the server runs on :5000 and the client on :8080. Below you can see my proxy. But it keeps using :8080 instead of :5000

vue.config.js

module.exports = {
    devServer: {
      proxy: {
        '/api': {
          target: 'http://localhost:5000',
          ws: true,
          changeOrigin: true
        }
      }
    }
  }

And this is an example of how I call a request

const url = '/api/races/'
const config = {
    headers: {'Authorization': "Bearer " + localStorage.token}
}

class RaceService {
    // Get Races
    static getRaces() {
        return new Promise(async (resolve, reject) => {
            try {
                const res = await axios.get(url)
                const races = res.data
                resolve(races)
            } catch (err) {
                reject(err)
            }
        })
    }
}

Can someone maybe help me? the api requests need to use :5000 instead of :8080

Upvotes: 9

Views: 13365

Answers (3)

Yugank Singh
Yugank Singh

Reputation: 506

try reloading your vue server if you haven't done so

until you start and stop the vue server cause until you do this the vue.config.js is not loaded in,

so do

Ctrl+C in the terminal where your you did npm run serve and then start again using npm run serve

Upvotes: -1

Gaurav Yadav
Gaurav Yadav

Reputation: 61

Check your vue.config.js file location it should not be in the src folder, instead at the root of your project, for details refer : This Link

Upvotes: 1

r0ulito
r0ulito

Reputation: 487

You'll have to use "^/api" instead of "/api"

Upvotes: 3

Related Questions