Commit 5dce753f authored by xzj's avatar xzj

统一参数为uri;write逻辑移到v2版本中

parent 6c8b87b8
...@@ -48,7 +48,7 @@ module.exports = router ...@@ -48,7 +48,7 @@ module.exports = router
pipe pipe
option: 对象格式,和request包参数一直 option: 对象格式,和request包参数一直
如果是字符串类型,默认会转为{url: 'some string'} 如果是字符串类型,默认会转为{uri: 'some string'}
extra: 对象格式,现有两个属性 extra: 对象格式,现有两个属性
rawRes: 是否返回接受到的res,默认false rawRes: 是否返回接受到的res,默认false
...@@ -63,7 +63,7 @@ extra: 对象格式,现有两个属性 ...@@ -63,7 +63,7 @@ extra: 对象格式,现有两个属性
if (ctx.method === 'POST') { if (ctx.method === 'POST') {
let option = { let option = {
url: config.api.mall_api + ctx.url.replace('/mall/other', ''), uri: config.api.mall_api + ctx.url.replace('/mall/other', ''),
headers: { headers: {
'x-auth-token': ctx.headers['x-auth-token'] 'x-auth-token': ctx.headers['x-auth-token']
} }
......
'use strict' 'use strict'
const request = require('request') const request = require('request')
const debug = require('debug')('request-proxy') const debug = require('debug')('request-proxy')
const qs = require('querystring')
const Agent = require('agentkeepalive'); const Agent = require('agentkeepalive');
const HttpsAgent = require('agentkeepalive').HttpsAgent; const HttpsAgent = require('agentkeepalive').HttpsAgent;
...@@ -49,11 +48,6 @@ module.exports = function (options) { ...@@ -49,11 +48,6 @@ module.exports = function (options) {
timeout: options.timeout timeout: options.timeout
} }
const isProduction = process.env.NODE_ENV === 'production'
const rewriteUrl = function (source, to) {
return source.replace(/\/\/([^\/]+)\//, `//${to}/`).replace(/_ip_=[\w\.:%]+&?/, '')
}
const httpAgent = new Agent(options.agent); const httpAgent = new Agent(options.agent);
const httpsAgent = new HttpsAgent(); const httpsAgent = new HttpsAgent();
...@@ -73,19 +67,11 @@ module.exports = function (options) { ...@@ -73,19 +67,11 @@ module.exports = function (options) {
option = Object.assign({}, curlDefault, option) option = Object.assign({}, curlDefault, option)
extra = Object.assign({}, extraDefault, extra) extra = Object.assign({}, extraDefault, extra)
if (!isProduction && ctx.request.query['_ip_']) {
if (option.url) {
option.url = rewriteUrl(option.url, ctx.request.query['_ip_'])
} else {
option.uri = rewriteUrl(option.uri, ctx.request.query['_ip_'])
}
}
debug(option) debug(option)
if (!option.agent && !/^https/.test(option.url)) { if (!option.agent && !/^https/.test(option.uri)) {
option.agent = httpAgent option.agent = httpAgent
} else if (!option.agent && /^https/.test(option.url)) { } else if (!option.agent && /^https/.test(option.uri)) {
option.agent = httpsAgent option.agent = httpsAgent
} }
...@@ -114,29 +100,21 @@ module.exports = function (options) { ...@@ -114,29 +100,21 @@ module.exports = function (options) {
} }
option = Object.assign({}, pipeDefault, option) option = Object.assign({}, pipeDefault, option)
if (!isProduction && ctx.request.query['_ip_']) {
if (option.url) {
option.url = rewriteUrl(option.url, ctx.request.query['_ip_'])
} else {
option.uri = rewriteUrl(option.uri, ctx.request.query['_ip_'])
}
}
if (!option.method) { if (!option.method) {
option.method = ctx.method option.method = ctx.method
} }
debug(option) debug(option)
if (!option.agent && !/^https/.test(option.url)) { if (!option.agent && !/^https/.test(option.uri)) {
option.agent = httpAgent option.agent = httpAgent
} else if (!option.agent && /^https/.test(option.url)) { } else if (!option.agent && /^https/.test(option.uri)) {
option.agent = httpsAgent option.agent = httpsAgent
} }
return new Promise(function (resolve, reject) { return new Promise(function (resolve, reject) {
let r = ctx.req.pipe(request(option, function (err, response) { let r = ctx.req.pipe(request(option, function (err, response) {
if (option.time && options.logger) { if (option.time && options.logger) {
options.logger.info(option.url || option.uri, JSON.stringify(response && response.timings || '')) options.logger.info(option.uri, JSON.stringify(response && response.timings || ''))
} }
})) }))
r.on('error', function (err) { r.on('error', function (err) {
......
{ {
"name": "request-proxy", "name": "request-proxy",
"version": "1.1.2", "version": "1.2.0",
"description": "", "description": "",
"main": "index.js", "main": "index.js",
"scripts": { "scripts": {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment