Skip to content

Commit

Permalink
* 修复默认数据在application内覆盖滞留的问题
Browse files Browse the repository at this point in the history
  • Loading branch information
h1bomb committed Feb 23, 2016
1 parent bda957f commit e1c2cc5
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 25 deletions.
5 changes: 4 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -103,4 +103,7 @@ v1.1.1

v1.1.2
* 修复递归创建目录,如果是绝对路径,创建成相对路径的问题,采用了mkdirp模块
* 完善单元测试,保证覆盖率100%
* 完善单元测试,保证覆盖率100%

v1.1.3
* 修复默认数据在application内覆盖,滞留的问题
28 changes: 13 additions & 15 deletions lib/mid/proxy.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,7 @@ var debug = require('debug')('mid-proxy');
var request = require('request');
var _ = require('lodash');
var util = require('../util');
var ret = {};
var count = 0;

/**
* 接口代理中间件
* @param {Request} req 请求对象
Expand All @@ -27,9 +25,8 @@ var count = 0;
module.exports = function(req, res, next) {
var input = req.input,
apiNum = 1;

ret = {};
count = 0;
req.apiCount = 0;
req.apiRet = {};
//没有入参,跳过
if (!input) {
next();
Expand All @@ -51,7 +48,6 @@ module.exports = function(req, res, next) {
req:req//express 请求对象
};


//根据接口配置调用
if (input.config.apis) {
apiNum = _.size(input.config.apis);
Expand Down Expand Up @@ -81,22 +77,24 @@ module.exports = function(req, res, next) {
function procRet(params,key) {
if(params.body) {
if (params.apiNum > 1) {
ret[key] = util.jsonParse(params.body,params.req.app);
params.req.apiRet[key] = util.jsonParse(params.body,params.req.app);
} else {
ret = util.jsonParse(params.body,params.req.app);
params.req.apiRet = util.jsonParse(params.body,params.req.app);
}
}

//默认返回数据存在,合并该数据到返回数据(以返回数据为主)
var defaultData = params.req.input.config.data;
if(defaultData) {
params.res.proxyData = _.merge(defaultData,ret);
params.res.proxyData = _.merge({},defaultData,params.req.apiRet);
} else {
params.res.proxyData = ret;
params.res.proxyData = params.req.apiRet;
}

count++;
if (params.apiNum === count) {

params.req.apiCount++;
if (params.apiNum === params.req.apiCount) {
delete params.req.apiCount;
delete params.req.apiRet;
params.req.app.yolog.api.log('info','server api after proc:',params.res.proxyData);
params.next();
}
Expand All @@ -119,7 +117,7 @@ function callApi(params,key) {
}

//缓存接口返回
if (params.res.getCache) {
if (params.res.getCache && params.api.cache) {

var cacheKey = params.res.genKey(params.domain, params.api.url, JSON.stringify(params.params));

Expand Down Expand Up @@ -185,7 +183,7 @@ function callServer(params,key) {
params.req.app.yolog.profile('proxy api time:'+options.url,'api');
if (response && response.statusCode === 200) {
params.req.app.yolog.api.log('verbose','server api status 200 output:',body);
if (params.res.setCache) {
if (params.res.setCache && params.api.cache) {
var cacheKey = params.res.genKey(params.domain, params.api.url, JSON.stringify(params.params));

params.res.setCache(cacheKey, body, params.api.cache);
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "yo.js",
"version": "1.1.2",
"version": "1.1.3",
"description": "Construction of the presentation layer solution is based on express, handlebars",
"main": "index.js",
"author": "hbomb",
Expand Down
17 changes: 9 additions & 8 deletions test/mid/proxy.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,18 +83,17 @@ describe('mid/proxy', function() {
domain: 'xxx',
body: '{}',
res: {},
req:reqMock({input:{config:{data:{}}}}),
req:reqMock({apiCount:0,apiRet:{},input:{config:{data:{}}}}),
next: function() {}
}
proxy.__get__('procRet')(params,'aaa');
expect(params.res.proxyData).to.have.property('aaa');
expect(proxy.__get__('count')).to.be(1);
expect(params.req.apiCount).to.be(1);
params.req.input.config.data = false;
proxy.__get__('procRet')(params,'aaa');
expect(params.res.proxyData).to.have.property('aaa');
});
it('如果接口数和调用次数相同,期待被next回调', function(done) {
proxy.__set__('count', 0);
var params = {
apiNum: 1,
api: {
Expand All @@ -104,7 +103,7 @@ describe('mid/proxy', function() {
domain: 'xxx',
body: '{}',
res: {},
req:reqMock({input:{config:{data:{}}}}),
req:reqMock({apiCount:0,apiRet:{},input:{config:{data:{}}}}),
next: function() {
done();
}
Expand All @@ -119,7 +118,7 @@ describe('mid/proxy', function() {
domain: 'xxx',
body: 'xxx',
res: {},
req:reqMock({input:{config:{data:{}}}}),
req:reqMock({apiCount:0,apiRet:{},input:{config:{data:{}}}}),
next: function() {
expect(params.res.proxyData).to.eql({});
done();
Expand All @@ -134,7 +133,7 @@ describe('mid/proxy', function() {
apiNum: 1,
domain: 'xxx',
res: {},
req:reqMock({input:{config:{data:{"a":1}}}}),
req:reqMock({apiCount:0,apiRet:{},input:{config:{data:{"a":1}}}}),
next: function() {
expect(params.res.proxyData).to.eql({"a":1});
done();
Expand Down Expand Up @@ -183,7 +182,8 @@ describe('mid/proxy', function() {
},
domain: 'xxx',
api: {
url: 'xxx'
url: 'xxx',
cache:100
},
req:reqMock({input:{config:{data:{}}}}),
params: {
Expand Down Expand Up @@ -211,7 +211,8 @@ describe('mid/proxy', function() {
req:reqMock({input:{config:{data:{}}}}),
domain: 'xxx',
api: {
url: 'xxx'
url: 'xxx',
cache:100
},
params: {

Expand Down

0 comments on commit e1c2cc5

Please sign in to comment.