前端調試利器---nproxy

來源:互聯網
上載者:User

標籤:blog   http   java   使用   os   io   檔案   資料   

前言:習慣了在windows環境中使用Fiddler的童鞋們,是不是感覺它的網路重新導向功能很酷,Fiddler能按照你設定的規制捕獲網路請求,再指向本地檔案,如攔截你的js檔案到本地,就能很快的調試線上環境(如後台環境,測試環境,預上線環境等)。但是Fiddler的使用對於初學者來說還是稍有困難的,介面功能很多,導致新手無從下手。(我當初就是這樣的),並且Fiddler雖然有Mac版本,但是問題很多,我試了好幾次都不行。

本文:今天給大家介紹一款新的神器,nproxy.它能通吃windows,linux.mac平台。而且使用及其簡單。一個設定檔就搞定了。

安裝:使用npm安裝即可

npm install -g nproxy

用法:在replace_rule.js中設定你要重新導向的檔案路徑即可,然後如下執行

nproxy -l replace_rule.js 

replace_rule.js樣本:

module.exports = [  // 1. replace single file with local one  {    pattern: ‘homepage.js‘,      // Match url you wanna replace    responder:  "/home/goddyzhao/workspace/homepage.js"  },  // 2. replace single file with web file  {    pattern: ‘homepage.js‘,      // Match url you wanna replace    responder:  "http://www.anotherwebsite.com/assets/js/homepage2.js"  },  // 3. replace combo file with src with absolute file path  {    pattern: ‘group/homepageTileFramework.*.js‘,     responder: [      ‘/home/goddyzhao/workspace/webapp/ui/homepage/js/a.js‘,      ‘/home/goddyzhao/workspace/webapp/ui/homepage/js/b.js‘,      ‘/home/goddyzhao/workspace/webapp/ui/homepage/js/c.js‘    ]   },  // 4. replace combo file with src with relative file path and specified dir  {    pattern: ‘group/homepageTileFramework.*.js‘,    responder: {      dir: ‘/home/goddyzhao/workspace/webapp/ui/homepage/js‘,      src: [        ‘a.js‘,        ‘b.js‘,        ‘c.js‘      ]    }  },  // 5. Map server image directory to local image directory  {    pattern: ‘ui/homepage/img‘,  // must be a string    responder: ‘/home/goddyzhao/image/‘ //must be a absolute directory path  },  // 6. Write responder with regular expression variables like $1, $2  {    pattern: /https?:\/\/[\w\.]*(?::\d+)?\/ui\/(.*)_dev\.(\w+)/,    responder: ‘http://localhost/proxy/$1.$2‘  },  // 7. Map server image directory to local image directory with regular expression  // This simple rule can replace multiple directories to corresponding locale ones  // For Example,   //   http://host:port/ui/a/img/... => /home/a/image/...  //   http://host:port/ui/b/img/... => /home/b/image/...  //   http://host:port/ui/c/img/... => /home/c/image/...  //   ...  {    pattern: /ui\/(.*)\/img\//,    responder: ‘/home/$1/image/‘  }];

  nproxy參數說明:

Options:  -h, --help         output usage information  -V, --version      output the version number  -l, --list [list]  Specify the replace rule file  -p, --port [port]  Specify the port nproxy will listen on(8989 by default)  -t, --timeout [timeout] Specify the request timeout (5 seconds by default)

改變預設監聽連接埠:因為要捕獲網路請求,nproxy必然會監聽請求地址和請求連接埠,它預設的監聽連接埠為8989,你可以通過-p參數來設定,如

nproxy -l replace_rule.js  -p 8181

  即可改變監聽的連接埠。

 瀏覽器代理設定:Proxy 位址為127.0.0.1 連接埠即為你要監聽的連接埠,如8181.至於怎麼設定瀏覽器的代理,請百度就知道了。

通過以上步驟,你就搭建起了前端調試環境。可以方便的直接調用後端的介面,而不用在本地造一大堆假資料了。是不是很方便了?

現在就來試試吧!

 

 

  

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.