欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

詳解Webpack-dev-server的proxy用法

 更新時(shí)間:2018年09月08日 10:25:13   作者:funnycoderstar  
這篇文章主要介紹了詳解Webpack-dev-server的proxy用法,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧

前言

如果你有單獨(dú)的后端開(kāi)發(fā)服務(wù)器 API,并且希望在同域名下發(fā)送 API 請(qǐng)求 ,那么代理某些 URL 會(huì)很有用。

解決開(kāi)發(fā)環(huán)境的跨域問(wèn)題(不用在去配置nginx和host, 爽歪歪~~)

在webpack.config.js中配置

下面簡(jiǎn)單介紹一下五個(gè)經(jīng)常使用的場(chǎng)景

使用一:

mmodule.exports = {
  //...
  devServer: {
    proxy: {
      '/api': 'http://localhost:3000'
    }
  }
};

請(qǐng)求到 /api/xxx 現(xiàn)在會(huì)被代理到請(qǐng)求 http://localhost:3000/api/xxx, 例如 /api/user 現(xiàn)在會(huì)被代理到請(qǐng)求 http://localhost:3000/api/user

使用二

如果你想要代碼多個(gè)路徑代理到同一個(gè)target下, 你可以使用由一個(gè)或多個(gè)「具有 context 屬性的對(duì)象」構(gòu)成的數(shù)組:

module.exports = {
  //...
  devServer: {
    proxy: [{
      context: ['/auth', '/api'],
      target: 'http://localhost:3000',
    }]
  }
};

使用三:

如果你不想始終傳遞 /api ,則需要重寫(xiě)路徑:

module.exports = {
  //...
  devServer: {
    proxy: {
      '/api': {
        target: 'http://localhost:3000',
        pathRewrite: {'^/api' : ''}
      }
    }
  }
};

請(qǐng)求到 /api/xxx 現(xiàn)在會(huì)被代理到請(qǐng)求 http://localhost:3000/xxx, 例如 /api/user 現(xiàn)在會(huì)被代理到請(qǐng)求 http://localhost:3000/user

使用四:

默認(rèn)情況下,不接受運(yùn)行在 HTTPS 上,且使用了無(wú)效證書(shū)的后端服務(wù)器。如果你想要接受,只要設(shè)置 secure: false 就行。修改配置如下:

module.exports = {
  //...
  devServer: {
    proxy: {
      '/api': {
        target: 'https://other-server.example.com',
        secure: false
      }
    }
  }
};

使用五:

有時(shí)你不想代理所有的請(qǐng)求。可以基于一個(gè)函數(shù)的返回值繞過(guò)代理。

在函數(shù)中你可以訪問(wèn)請(qǐng)求體、響應(yīng)體和代理選項(xiàng)。必須返回 false 或路徑,來(lái)跳過(guò)代理請(qǐng)求。

例如:對(duì)于瀏覽器請(qǐng)求,你想要提供一個(gè) HTML 頁(yè)面,但是對(duì)于 API 請(qǐng)求則保持代理。你可以這樣做:

module.exports = {
 //...
  devServer: {
    proxy: {
      '/api': {
        target: 'http://localhost:3000',
        bypass: function(req, res, proxyOptions) {
          if (req.headers.accept.indexOf('html') !== -1) {
            console.log('Skipping proxy for browser request.');
            return '/index.html';
          }
        }
      }
    }
  }  
};

解決跨域原理

上面的參數(shù)列表中有一個(gè)changeOrigin參數(shù), 是一個(gè)布爾值, 設(shè)置為true, 本地就會(huì)虛擬一個(gè)服務(wù)器接收你的請(qǐng)求并代你發(fā)送該請(qǐng)求,

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

vue-cli中proxyTable配置接口地址代理示例

修改 config/index.js

module.exports = {
  dev: {
  // 靜態(tài)資源文件夾
  assetsSubDirectory: 'static',
  // 發(fā)布路徑
  assetsPublicPath: '/',

  // 代理配置表,在這里可以配置特定的請(qǐng)求代理到對(duì)應(yīng)的API接口
  // 使用方法:https://vuejs-templates.github.io/webpack/proxy.html
  proxyTable: {
    // 例如將'localhost:8080/api/xxx'代理到'https://wangyaxing.cn/api/xxx'
    '/api': {
      target: 'https://wangyaxing.cn', // 接口的域名
      secure: false, // 如果是https接口,需要配置這個(gè)參數(shù)
      changeOrigin: true, // 如果接口跨域,需要進(jìn)行這個(gè)參數(shù)配置
    },
    // 例如將'localhost:8080/img/xxx'代理到'https://cdn.wangyaxing.cn/xxx'
    '/img': {
      target: 'https://cdn.wangyaxing.cn', // 接口的域名
      secure: false, // 如果是https接口,需要配置這個(gè)參數(shù)
      changeOrigin: true, // 如果接口跨域,需要進(jìn)行這個(gè)參數(shù)配置
      pathRewrite: {'^/img': ''} // pathRewrite 來(lái)重寫(xiě)地址,將前綴 '/api' 轉(zhuǎn)為 '/'。
    }
  },
  // Various Dev Server settings
  host: 'localhost', // can be overwritten by process.env.HOST
  port: 4200, // can be overwritten by process.env.PORT, if port is in use, a free one will be determined
}

更多參數(shù)

dev-server 使用了非常強(qiáng)大的http-proxy-middleware , http-proxy-middleware 基于 http-proxy 實(shí)現(xiàn)的,可以查看 http-proxy 的源碼和文檔:https://github.com/nodejitsu/node-http-proxy。

  • target:要使用url模塊解析的url字符串
  • forward:要使用url模塊解析的url字符串
  • agent:要傳遞給http(s).request的對(duì)象(請(qǐng)參閱Node的https代理和http代理對(duì)象)
  • ssl:要傳遞給https.createServer()的對(duì)象
  • ws:true / false,是否代理websockets
  • xfwd:true / false,添加x-forward標(biāo)頭
  • secure:true / false,是否驗(yàn)證SSL Certs
  • toProxy:true / false,傳遞絕對(duì)URL作為路徑(對(duì)代理代理很有用)
  • prependPath:true / false,默認(rèn)值:true - 指定是否要將目標(biāo)的路徑添加到代理路徑
  • ignorePath:true / false,默認(rèn)值:false - 指定是否要忽略傳入請(qǐng)求的代理路徑(注意:如果需要,您必須附加/手動(dòng))。
  • localAddress:要為傳出連接綁定的本地接口字符串
  • changeOrigin:true / false,默認(rèn)值:false - 將主機(jī)標(biāo)頭的原點(diǎn)更改為目標(biāo)URL

參考

官方文檔
http-proxy-middleware
node-http-proxy
API Proxying During Development

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論