Authored by 周奇琪

灰度中间件加files的处理

@@ -6,6 +6,7 @@ var request = require('request'); @@ -6,6 +6,7 @@ var request = require('request');
6 6
7 var config = require('../config/gray'); 7 var config = require('../config/gray');
8 var _ = require('lodash'); 8 var _ = require('lodash');
  9 +var fs = require('fs');
9 10
10 /** 11 /**
11 * 灰度界面配置 12 * 灰度界面配置
@@ -54,7 +55,11 @@ module.exports = function(proxyRoute) { @@ -54,7 +55,11 @@ module.exports = function(proxyRoute) {
54 } else { 55 } else {
55 options.headers['cookie'] = req.session.gray; 56 options.headers['cookie'] = req.session.gray;
56 } 57 }
57 - 58 + //如果是上传文件添加文件信息到form表单
  59 + if(req.files && _.keys(req.files).length>0) {
  60 + options.formData = addFiles(req);
  61 + delete options.form;
  62 + }
58 logger.log('info','grayroute: request options: %j',options,{}); 63 logger.log('info','grayroute: request options: %j',options,{});
59 64
60 //发起代理请求 65 //发起代理请求
@@ -89,3 +94,16 @@ module.exports = function(proxyRoute) { @@ -89,3 +94,16 @@ module.exports = function(proxyRoute) {
89 } 94 }
90 } 95 }
91 } 96 }
  97 +
  98 + /**
  99 + * 添加文件到body中
  100 + * @param {Object} req 请求对象
  101 + */
  102 + function addFiles(req) {
  103 + var body = {};
  104 + _.forEach(req.files,function(v,k){
  105 + body[k] = fs.createReadStream(v.path);
  106 + });
  107 + _.merge(body,req.body);
  108 + return body;
  109 + }