在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称:sunyongjian/koa2-proxy-middleware开源软件地址:https://github.com/sunyongjian/koa2-proxy-middleware开源编程语言:JavaScript 100.0%开源软件介绍:IntroductionA koa2 middleware by means of http-proxy-middleware. Install
UsageThis is a very simple usage. If you are familiar with the use of http-proxy-middleware and path-to-regexp. Exampleconst Koa = require('koa');
const proxy = require('koa2-proxy-middleware');
const bodyparser = require('koa-bodyparser');
const app = new Koa();
const options = {
targets: {
'/user': {
// this is option of http-proxy-middleware
target: 'http://localhost:3000', // target host
changeOrigin: true, // needed for virtual hosted sites
},
'/user/:id': {
target: 'http://localhost:3001',
changeOrigin: true,
},
// (.*) means anything
'/api/(.*)': {
target: 'http://10.94.123.123:1234',
changeOrigin: true,
pathRewrite: {
'/passager/xx': '/mPassenger/ee', // rewrite path
}
},
}
}
app.use(proxy(options));
app.use(bodyparser({
enableTypes:['json', 'form', 'text']
})); options{
targets: {
[key]: [option],
},
} keyThe key route that you need to proxy.And through path-to-regexp into regexp. This is mean: const regexp = pathToRegexp('route-key');
regexp.test(ctx.req.path); optionThe option corresponding to key and like http-proxy-middleware. TipsBodyparser need to after proxy-middleware when request method is POST. Otherwise there will be a delay. I think this is a bug of bodyparser. |
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论