I'm using the following configuration in my vue.config.js located in the root of my repository and it's not working.
module.exports = {
devServer: {
proxy: "http://localhost:3030"
}
}
and this is how I'm trying to call it
return await fetch("/posts", options).then((response) =>
response.json()
).catch(e => console.log("Error fetching posts", e));
however when I change the calling code to the code shown below everything works
return await fetch("http://localhost:3030/posts", options).then((response) =>
response.json()
).catch(e => console.log("Error fetching posts", e));
Edit:
I should have mentioned that I'm using Vite for builds as that was causing some other problems for me with environment variables so it's possible they are causing problems with proxies too.
I looked into this more and it turns out that Vite does have proxy features and so I tried updating my code to use their proxy with still no luck.
// vite.config.js
import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
// https://vitejs.dev/config/
export default defineConfig({
plugins: [vue()],
server: {
"/posts": "http://localhost:3030/posts"
}
})
proxytakes an object as the argument, where the property 'key' is a path. What are you trying to achieve here?fetch("/posts", options)to resolve tohttp://localhost:3030/postsin dev builds