1.0.0 • Published 4 years ago
@eggjs/egg-http-proxy v1.0.0
egg-http-proxy
A simple http proxy base on egg httpclient.
Install
$ npm i egg-http-proxy --save
// {app_root}/config/plugin.js
exports.httpProxy = {
enable: true,
package: 'egg-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';
},
});
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
1.0.0
4 years ago