title: Vue: Proxy requests to a separate backend server with vue-cli
body: <p>I am using vue-cli webpack template to generate my projects, and I'd like to proxy requests to a separate, backend server. But I got the error message as follow.</p>
  
  <p>Could anyone tell me what's the matter with my coding? </p>
  
  <p>Thank you very much!</p>
  
  <p>Error message</p>
  
  <pre><code>[HPM] Error occurred while trying to proxy request from localhost:8080 to http://localhost:3000 (ECONNREFUSED) (https://nodejs.org/api/errors.html#errors_common_system_errors)
  </code></pre>
  
  <p>config -> index.js</p>
  
  <pre><code>proxyTable: {
  '/api':{
      target: 'http://localhost:3000',
      changeOrigin: true,
      pathRewrite: {
      '^/api': ''
      }
   }
  </code></pre>
  
  <p>src -> main.js</p>
  
  <pre><code>import Vue from 'vue'
  import App from './App'
  import VueRouter from 'vue-router'
  import routerConfig from './router.config.js' 
  import Axios from 'axios'
  
  
  Vue.config.productionTip = false;
  Vue.prototype.$axios = Axios;
  Vue.prototype.HOST = '/api';
  
  Vue.use(VueRouter);
  
  const router = new VueRouter(routerConfig) 
  
  new Vue({
   el: '#app',
   router,  
   components: { App }, 
   template: '&lt;App/&gt;',
  })
  </code></pre>
  
  <p>src -> App.vue</p>
  
  <pre><code>export default{
  
  created(){
    var url = this.HOST
    this.$axios.get(url,{
  
    }).then((res)=&gt;{
  
     console.log(res.data)
  
    },(res)=&gt;{
  
    alert(res.status)
    })
   }
  }
  </code></pre>
  
  <p>server</p>
  
  <pre><code>const express = require('express');
  const mysql = require('mysql');
  
  const db = mysql.createPool({
    localhost:'localhost',
    user:'root',
    password:'123456',
    database:'blog'
  })
  
  const server = express();
  
  server.use('/api',(req,res)=&gt;{
  
  db.query(`SELECT * FROM articles_table`,(err,data)=&gt;{
     if(err){
       console.error(err);
       res.status(500).send('database error').end();
     }else{
       res.send(data)
     }
   })
  
  })
  server.listen(3000)
  </code></pre>