36

I am using vue-cli webpack-simple template to generate my projects, and I'd like to proxy requests to a separate, backend server. How can this be easily achieved?

1
  • 1
    I didn't really take a close look at vue-cli's boilerplate and used this generator for yeoman instead. It got me started even I had little knowledge of webpack/gulp/browser-sync(though there was a few gotchas when I tried to integrate it with backend). I think it's worth a look and choosing between vue-cli and it. Commented Oct 30, 2016 at 4:02

4 Answers 4

65

In @vue/cli 3.x:

  • Create a vue.config.js file in the root folder of your project, if you don't already have one.
  • Have its contents as follows:
// vue.config.js
module.exports = {
  devServer: {
    proxy: {
      "/gists": {
        target: "https://api.github.com",
        secure: false
      }
    }
  }
};

Now any call to (assuming your dev server is at localhost:8080) http://localhost:8080/gists will be redirected to https://api.github.com/gists.


Another example: proxying all calls

Say you have a local backend server that is typically deployed at localhost:5000 and you want to redirect all calls to /api/anything to it. Use:

// vue.config.js
module.exports = {
    devServer: {
        proxy: {
            "/api/*": {
                target: "http://localhost:5000",
                secure: false
            }
        }
    }
};
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks, this helped me. Annoying how it's not super easy to find this info with numerous google searches
Can anyone link to the docs where I should have found this information?
Relevant devServer Webpack documentation: webpack.js.org/configuration/dev-server/#devserverproxy
30

If you use webpack template with vue-cli, then you can find the required information in this reference document:

http://vuejs-templates.github.io/webpack/proxy.html

Or instead of changing your template now, you may copy the relevant config from the webpack template into your local webpack-simple template.

EDIT: more info from my local setup

This is what I have in my config/index.js under module.exports:

dev: {
    env: require('./dev.env'),
    port: 4200,
    assetsSubDirectory: 'static',
    assetsPublicPath: '/',
    proxyTable: {
        '/api': {
            target: 'http://localhost:8080',
            changeOrigin: true
        },
        '/images': {
            target: 'http://localhost:8080',
            changeOrigin: true
        },
        // and so on...

The above config runs my vue-cli on port 4200, and I run my server on port 8080.

EDIT: Corrected info about CORS after comment #4 and #5

Note:

  • The changeOrigin option in dev.proxyTable (in webpack config) ensures that you do not need to serve CORS headers on your server API responses.
  • If you decide to omit changeOrigin for any reason, then you need to ensure that your server API includes Access-Control-Allow-Origin: * (or equivalent) in its response headers.

References:

  1. https://stackoverflow.com/a/36662307/654825
  2. https://github.com/chimurai/http-proxy-middleware

6 Comments

So it's more difficult to using the webpack-simple template?
I think webpack-simple is intended to get started with Vue.js. The webpack template provides lot more configuration, and even has a detailed help-page for additional info.
As you rightly said, it becomes more difficult later if you start with the simple template and write a lot of code, as you need to do all the additional config work yourself.
We may not need CORS here, I added static file plugin/library to my backend so it can serve index.html and made the frontend develop server proxy every single request to backend server. No more CORS, a bit cleaner.
Thanks for the info! I just did some quick experiment locally and found that either we need CORS headers, or set changeOrigin: true in the proxyTable options for webpack dev. Server proxy is done using http-proxy-middleware - this github page confirms that changeOrigin option changes host headers to server URL (target).
|
6
module.exports = {

    devServer: {
        proxy: {
            '/api': {
                target: 'http://genius.net',
                changeOrigin: true,
                pathRewrite: {
                    '^/api': ''
                }
            },
            '/auth': {
                target: 'http://genius23.net',
                changeOrigin: true,
                pathRewrite: {
                    '^/auth': ''
                }
            },
        }
    }

};

1 Comment

This is the best answer because it gives the pathRewrite. Unless you are adding /api to the URI of all of your dev servers you will need this.
0

if these answers do not work

try to add http://127.0.0.1:{your port} in my case it would not start without it

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.