/**
 * 代理请求，针对ajax直接请求api(未配置router)，和router设置了proxy属性
 * 实践：
 * 1、ajax直接请求了未配置为router的后端api路径，可直接转发给后端api
 * 2、router设置了proxy属性为true，例如 验证码接口，需转发后端生成的captcha图片
 *
 * @param  {[type]} req    [description]
 * @param  {[type]} res    [description]
 * @return {[type]}        [description]
 */
const parseURLMethod = require('../utils/parse-url-method');

function requestProxy(req, res, next) {
  const router = req.router;
  const xhrProxy = !router && req.xhr;
  const routeProxy = router && router.proxy;

  // 既不是ajax，又不是代理
  if (!routeProxy && !xhrProxy) {
    next();
    return;
  }

  let url = req.path;
  let method = req.method.toLowerCase();

  // 如果是get请求，为什么不用redirect跳转
  // 避免redirect url不支持对外访问
  if (routeProxy && router.api) {
    const urlMethod = parseURLMethod(router.api, method);

    url = urlMethod.url;
    method = urlMethod.method;
  }

  url = this.get('handleAPI')(url, req);

  const request = req.httpRequest();
  const options = {
    url,
    'qs': req.query,
    'body': req.body
  };

  res.proxyResoponse = request[method](options).on('response', response => {
    res.set(response.headers);
  });

  next.to('response');
}

module.exports = requestProxy;

@@ -284,16 +284,46 @@
 on%5D%0A */%0A
+const log = require('t-log');%0A
 const pa
@@ -530,16 +530,134 @@
 proxy;%0A%0A
+  let url = req.pathname; // pathname %E6%94%AF%E6%8C%81forward%E4%BB%A3%E7%90%86%0A  const isDomainProxy = url.startsWith('http') && res.forwardSent;%0A%0A
   // %E6%97%A2%E4%B8%8D%E6%98%AF
@@ -697,16 +697,34 @@
 xhrProxy
+ && !isDomainProxy
 ) %7B%0A    
@@ -752,30 +752,8 @@
  %7D%0A%0A
-  let url = req.path;%0A
   le
@@ -819,16 +819,17 @@
 direct%E8%B7%B3%E8%BD%AC
+:
 %0A  // %E9%81%BF%E5%85%8D
@@ -1158,16 +1158,155 @@
 y%0A  %7D;%0A%0A
+  // %E8%AE%B0%E5%BD%95apiInfo%E6%95%B0%E6%8D%AE%0A  res.apiInfo.proxy = %7B%0A    api: req.pathname,%0A    query: req.query,%0A    body: req.body%0A  %7D%0A%0A  const timer = log.time();%0A%0A
   res.pr
@@ -1404,16 +1404,116 @@
 eaders);
+%0A%0A    res.apiInfo.proxy.headers = response.headers;%0A    res.apiInfo.proxy.consumeTime = timer.end();
 %0A  %7D);%0A%0A

update proxy other site and add apiInfo data