...
|
...
|
@@ -5,6 +5,8 @@ |
|
|
var request = require('request');
|
|
|
|
|
|
var config = require('../config/gray');
|
|
|
var _ = require('lodash');
|
|
|
var queryString = require('query-string');
|
|
|
|
|
|
/**
|
|
|
* 灰度界面配置
|
...
|
...
|
@@ -66,14 +68,49 @@ module.exports = function(proxyRoute) { |
|
|
options.headers['Referer'] = req.headers['referer'];
|
|
|
}
|
|
|
|
|
|
if(isFromArray(req.body)) {
|
|
|
options.method = "GET";
|
|
|
options.url += "?"+ queryString.stringify(req.body);
|
|
|
}
|
|
|
|
|
|
//如果是POST,设置form表单的传参
|
|
|
if(method === "POST") {
|
|
|
options.form = req.body;
|
|
|
}
|
|
|
|
|
|
logger.log('info','grayroute: request options: %j',options,{});
|
|
|
|
|
|
//发起代理请求,直接使用pipe
|
|
|
//发起代理请求
|
|
|
if(req.is('html')) { //如果是静态资源
|
|
|
request(options,callback);
|
|
|
} else {
|
|
|
request(options).pipe(ress);
|
|
|
}
|
|
|
|
|
|
//代理回调
|
|
|
function callback(err,res,body) {
|
|
|
if(err) {
|
|
|
logger.log('error','grayroute: request error:',err);
|
|
|
ress.status(500).send('');
|
|
|
} else if(res&&res.statusCode === 200) {
|
|
|
setGrayCookie(req,res);
|
|
|
var ContentType = res.caseless.get('Content-Type');
|
|
|
ress.append('Content-Type',ContentType);
|
|
|
ress.send(body);
|
|
|
|
|
|
} else if(res&&res.statusCode === 302) {
|
|
|
var curCookie = setGrayCookie(req,res);
|
|
|
if(!curCookie||curCookie.length<1) {
|
|
|
res.session.user = null;
|
|
|
}
|
|
|
//跳转
|
|
|
ress.redirect(res.caseless.get('location'));
|
|
|
} else if(res){
|
|
|
ress.status(res.statusCode).send(body);
|
|
|
} else {
|
|
|
ress.status(500).send('500 ERROR!');
|
|
|
}
|
|
|
}
|
|
|
} else {
|
|
|
|
|
|
//当不是老系统的URL,需要访问老系统保持session
|
...
|
...
|
@@ -93,6 +130,7 @@ module.exports = function(proxyRoute) { |
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
/**
|
|
|
* 灰度cookie设置
|
|
|
* @param {Object} req express request
|
...
|
...
|
@@ -107,3 +145,14 @@ module.exports = function(proxyRoute) { |
|
|
}
|
|
|
return cookie;
|
|
|
}
|
|
|
|
|
|
function isFromArray(form) {
|
|
|
form = form||{};
|
|
|
var ret = false;
|
|
|
_.forEach(form,function(v,k){
|
|
|
if(k.indexOf('[]')>-1){
|
|
|
ret = true;
|
|
|
}
|
|
|
});
|
|
|
return ret;
|
|
|
} |
|
|
\ No newline at end of file |
...
|
...
|
|