Skip to content

eggjs/egg-http-proxy

Repository files navigation

@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

About

http proxy for egg

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Contributors 4

  •  
  •  
  •  
  •