nuxt.js prxoy代理

156 阅读1分钟

本文已参与「新人创作礼」活动,一起开启掘金创作之路。 npm i @nuxtjs/proxy -D 在nuxt.config.js中配置

  modules: [
    '@nuxtjs/proxy',
  ],
在nuxt.confo.js中配置 proxy对象
 // 数组写法
  proxy: [
    [
      '/api', 
      { 
        target: 'http://localhost:3001', // api主机
        pathRewrite: { '^/api' : '' }
      }
  ],
    [
      '/age', 
      { 
        target: 'http://localhost:3002', // api主机
        // pathRewrite: { '^/api' : '/' }
      }
  ]
],
//对象写法
  // proxy: {
  //   "/name": {
  //     target: 'http://127.0.0.1:3001' 
  //   },
  //   '/age': {
  //     target: 'http://127.0.0.1:3002'
  //   },
  // }