Showing
1 changed file
with
50 additions
and
1 deletions
@@ -5,6 +5,8 @@ | @@ -5,6 +5,8 @@ | ||
5 | var request = require('request'); | 5 | var request = require('request'); |
6 | 6 | ||
7 | var config = require('../config/gray'); | 7 | var config = require('../config/gray'); |
8 | +var _ = require('lodash'); | ||
9 | +var queryString = require('query-string'); | ||
8 | 10 | ||
9 | /** | 11 | /** |
10 | * 灰度界面配置 | 12 | * 灰度界面配置 |
@@ -66,14 +68,49 @@ module.exports = function(proxyRoute) { | @@ -66,14 +68,49 @@ module.exports = function(proxyRoute) { | ||
66 | options.headers['Referer'] = req.headers['referer']; | 68 | options.headers['Referer'] = req.headers['referer']; |
67 | } | 69 | } |
68 | 70 | ||
71 | + if(isFromArray(req.body)) { | ||
72 | + options.method = "GET"; | ||
73 | + options.url += "?"+ queryString.stringify(req.body); | ||
74 | + } | ||
75 | + | ||
69 | //如果是POST,设置form表单的传参 | 76 | //如果是POST,设置form表单的传参 |
70 | if(method === "POST") { | 77 | if(method === "POST") { |
71 | options.form = req.body; | 78 | options.form = req.body; |
72 | } | 79 | } |
80 | + | ||
73 | logger.log('info','grayroute: request options: %j',options,{}); | 81 | logger.log('info','grayroute: request options: %j',options,{}); |
74 | 82 | ||
75 | - //发起代理请求,直接使用pipe | 83 | + //发起代理请求 |
84 | + if(req.is('html')) { //如果是静态资源 | ||
85 | + request(options,callback); | ||
86 | + } else { | ||
76 | request(options).pipe(ress); | 87 | request(options).pipe(ress); |
88 | + } | ||
89 | + | ||
90 | + //代理回调 | ||
91 | + function callback(err,res,body) { | ||
92 | + if(err) { | ||
93 | + logger.log('error','grayroute: request error:',err); | ||
94 | + ress.status(500).send(''); | ||
95 | + } else if(res&&res.statusCode === 200) { | ||
96 | + setGrayCookie(req,res); | ||
97 | + var ContentType = res.caseless.get('Content-Type'); | ||
98 | + ress.append('Content-Type',ContentType); | ||
99 | + ress.send(body); | ||
100 | + | ||
101 | + } else if(res&&res.statusCode === 302) { | ||
102 | + var curCookie = setGrayCookie(req,res); | ||
103 | + if(!curCookie||curCookie.length<1) { | ||
104 | + res.session.user = null; | ||
105 | + } | ||
106 | + //跳转 | ||
107 | + ress.redirect(res.caseless.get('location')); | ||
108 | + } else if(res){ | ||
109 | + ress.status(res.statusCode).send(body); | ||
110 | + } else { | ||
111 | + ress.status(500).send('500 ERROR!'); | ||
112 | + } | ||
113 | + } | ||
77 | } else { | 114 | } else { |
78 | 115 | ||
79 | //当不是老系统的URL,需要访问老系统保持session | 116 | //当不是老系统的URL,需要访问老系统保持session |
@@ -93,6 +130,7 @@ module.exports = function(proxyRoute) { | @@ -93,6 +130,7 @@ module.exports = function(proxyRoute) { | ||
93 | } | 130 | } |
94 | } | 131 | } |
95 | 132 | ||
133 | + | ||
96 | /** | 134 | /** |
97 | * 灰度cookie设置 | 135 | * 灰度cookie设置 |
98 | * @param {Object} req express request | 136 | * @param {Object} req express request |
@@ -107,3 +145,14 @@ module.exports = function(proxyRoute) { | @@ -107,3 +145,14 @@ module.exports = function(proxyRoute) { | ||
107 | } | 145 | } |
108 | return cookie; | 146 | return cookie; |
109 | } | 147 | } |
148 | + | ||
149 | + function isFromArray(form) { | ||
150 | + form = form||{}; | ||
151 | + var ret = false; | ||
152 | + _.forEach(form,function(v,k){ | ||
153 | + if(k.indexOf('[]')>-1){ | ||
154 | + ret = true; | ||
155 | + } | ||
156 | + }); | ||
157 | + return ret; | ||
158 | +} |
-
Please register or login to post a comment