Skip to content
This repository has been archived by the owner on Jan 14, 2020. It is now read-only.

Commit

Permalink
build
Browse files Browse the repository at this point in the history
  • Loading branch information
ITJesse committed Oct 31, 2016
1 parent 9c36c62 commit 92d7493
Showing 1 changed file with 22 additions and 34 deletions.
56 changes: 22 additions & 34 deletions dist/modules/proxy/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,6 @@ var _regenerator = require('babel-runtime/regenerator');

var _regenerator2 = _interopRequireDefault(_regenerator);

var _promise = require('babel-runtime/core-js/promise');

var _promise2 = _interopRequireDefault(_promise);

var _request = require('request');

var _request2 = _interopRequireDefault(_request);
Expand All @@ -34,30 +30,8 @@ var _common2 = _interopRequireDefault(_common);

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

// 封装 request post
var post = function post(url, headers, body) {
var options = {
url: url,
headers: headers,
method: 'post',
encoding: null,
gzip: true
};
if (!!body) {
options.body = body;
}

return new _promise2.default(function (resolve, reject) {
_common2.default.sendRequest(options).then(function (res) {
return resolve(res);
}).catch(function (err) {
return reject(err);
});
});
};

var middleware = function _callee(ctx, next) {
var req, res, ip, url, rawBody, result, headers, body;
var req, res, ip, url, rawBody, options, result, headers, body;
return _regenerator2.default.async(function _callee$(_context) {
while (1) {
switch (_context.prev = _context.next) {
Expand All @@ -66,7 +40,7 @@ var middleware = function _callee(ctx, next) {
res = ctx.reponse;

if (!(req.method == 'POST')) {
_context.next = 20;
_context.next = 23;
break;
}

Expand All @@ -83,10 +57,24 @@ var middleware = function _callee(ctx, next) {

case 8:
rawBody = _context.sent;
_context.next = 11;
return _regenerator2.default.awrap(post(url, req.headers, rawBody));

case 11:

delete req.headers['x-real-ip'];
options = {
url: url,
headers: req.headers,
method: 'post',
encoding: null,
gzip: true
};

if (!!rawBody) {
options.body = rawBody;
}
_context.next = 14;
return _regenerator2.default.awrap(_common2.default.sendRequest(url, options));

case 14:
result = _context.sent;
headers = result.res.headers;
body = result.body;
Expand All @@ -97,15 +85,15 @@ var middleware = function _callee(ctx, next) {
ctx.defaultBody = body;

// console.log("before: " + ctx.defaultBody);
_context.next = 19;
_context.next = 22;
return _regenerator2.default.awrap(next());

case 19:
case 22:
// console.log("after: " + ctx.defaultBody);

ctx.body = ctx.defaultBody;

case 20:
case 23:
case 'end':
return _context.stop();
}
Expand Down

0 comments on commit 92d7493

Please sign in to comment.