前端調試利器---nproxy


前言:習慣了在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

  即可改變監聽的端口。

 瀏覽器代理設置:代理地址為127.0.0.1 端口即為你要監聽的端口,如8181.至於怎么設置瀏覽器的代理,請百度就知道了。

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

現在就來試試吧!

 

 

  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM