Commit 2029ef9b authored by xzj's avatar xzj

支持_ip_参数,重写请求

parent 76c67b5d
'use strict'
const request = require('request')
const debug = require('debug')('request-proxy')
const qs = require('querystring')
const Agent = require('agentkeepalive');
const HttpsAgent = require('agentkeepalive').HttpsAgent;
......@@ -48,6 +49,11 @@ module.exports = function (options) {
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 httpsAgent = new HttpsAgent();
......@@ -67,6 +73,10 @@ module.exports = function (options) {
option = Object.assign({}, curlDefault, option)
extra = Object.assign({}, extraDefault, extra)
if (!isProduction && ctx.request.query['_ip_']) {
option.url = rewriteUrl(option.url, ctx.request.query['_ip_'])
}
debug(option)
if (!option.agent && !/^https/.test(option.url)) {
......@@ -100,6 +110,10 @@ module.exports = function (options) {
}
option = Object.assign({}, pipeDefault, option)
if (!isProduction && ctx.request.query['_ip_']) {
option.url = rewriteUrl(option.url, ctx.request.query['_ip_'])
}
if (!option.method) {
option.method = ctx.method
}
......
{
"name": "request-proxy",
"version": "1.0.4",
"version": "1.0.5",
"description": "",
"main": "index.js",
"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