Awesome Open Source
Awesome Open Source

@eggjs/http-proxy

NPM version build status Test coverage David deps Known Vulnerabilities npm download

A simple http proxy base on egg httpclient.

Install

$ npm i @eggjs/http-proxy --save
// {app_root}/config/plugin.js
exports.httpProxy = {
  enable: true,
  package: '@eggjs/http-proxy',
};

Usage

/**
 * @param {String} host - target host.
 * @param {Object} [options] - options for request, see https://github.com/node-modules/urllib
 * @param {Boolean} [options.withCredentials] - if true, will send cookie when cors
 * @param {Function} [options.rewrite] - rewrite target url obj
 */
await ctx.proxyRequest(host, options);

target host

// current url: http://eggjs.org/api/user -> http://github.com/api/user
await ctx.proxyRequest('github.com');

rewrite path

// current url: http://eggjs.org/api/user -> http://github.com/egg/eggjs/api/user
await ctx.proxyRequest('github.com', {
  rewrite(urlObj) {
    urlObj.pathname = '/egg/eggjs/api/user';
    return urlObj;
  },
});

modify response

await ctx.proxyRequest('github.com', {

  streaming: false,

  async beforeResponse(proxyResult) {
    proxyResult.headers.addition = 'true';
    // streaming=false should modify `data`, otherwise use stream to handler proxyResult.res yourself
    proxyResult.data = proxyResult.data.replace('github.com', 'www.github.com');
    return proxyResult;
  },
});

Configuration

// {app_root}/config/config.default.js
exports.httpProxy = {

};

see config/config.default.js for more detail.

Questions & Suggestions

Please open an issue here.

License

MIT

Alternatives To Egg Http Proxy
Select To Compare


Alternative Project Comparisons
Related Awesome Lists
Top Programming Languages

Get A Weekly Email With Trending Projects For These Topics
No Spam. Unsubscribe easily at any time.
Javascript (1,155,559
Http (30,284
Proxy (24,697
Http Client (3,160
Egg (2,208
Http Proxy (1,010
Eggjs (493
Egg Plugin (235