egg-http-proxy-plus
v2.0.1
Published
powerfull proxy middleware plugin for egg
Downloads
15
Maintainers
Readme
egg-http-proxy-plus
支持转发文件上传接口,支持自定义匹配方法,ctx 透传
Configure proxy middleware for egg. Use http-proxy-middleware.
Install
# use npm
$ npm i egg-http-proxy-plus --save
# use yarn
$ yarn add egg-http-proxy-plus
Usage
// {app_root}/config/plugin.js
exports.httpProxyPlus = {
enable: true,
package: 'egg-http-proxy-plus'
}
Configuration
Proxy /api
requests to http://www.example.org
// {app_root}/config/config.default.js
exports.httpProxyPlus = {
'/api': 'http://www.example.org'
}
// or
exports.httpProxyPlus = [
{
origin: '/api',
options: 'http://www.example.org'
}
]
A request to /api/users
will now proxy the request to http://www.example.org/api/users
If you don't want /api
to be passed along, we need to rewrite the path:
// {app_root}/config/config.default.js
exports.httpProxyPlus = {
'/api': {
target: 'http://www.example.org',
pathRewrite: { '^/api': '' }
}
}
// or
exports.httpProxyPlus = [
{
origin: '/api',
options: {
target: 'http://www.example.org',
pathRewrite: { '^/api': '' }
// ...
}
}
]
custom matching
For full control you can provide a custom function to determine which requests should be proxied or not.
// {app_root}/config/config.default.js
exports.httpProxyPlus = [
{
origin(pathname, req) {
return pathname.match('^/api') && req.method === 'GET'
},
options: {}
}
]
http-proxy events
Pay attention to the fourth parameter, the plug-in transparently transmits the ctx context
// {app_root}/config/config.default.js
exports.httpProxyPlus = {
'/api': {
target: 'http://www.example.org',
onProxyReq(proxyReq, req, res, ctx) {
if (req.method.toLowerCase() === 'post') {
const token = ctx.cookies.get('access_token')
token && proxyReq.setHeader('authorization', token)
}
}
}
}
For more advanced usages, checkout http-proxy-middleware options documentation.
Questions & Suggestions
Please open an issue here.