http-proxy-middleware is compatible with the following servers:
connect
express
browser-sync
lite-server
grunt-contrib-connect
grunt-browser-sync
gulp-connect
gulp-webserver
Please make it‘s compatible with Koa2.0, THANK YOU !
@fengxinming
Found a working setup using koa2-connect as an adapter.
Setup:
node v7.7.1
koa v2.2.0
koa-router v7.1.1
koa2-connect v1.0.2
http-proxy-middleware v0.17.4
Example:
const Koa = require('koa')
const Router = require('koa-router')
const c2k = require('koa2-connect')
const proxy = require('http-proxy-middleware')
var router = new Router()
router.get('*', c2k(proxy({target: 'http://api.icndb.com', changeOrigin:true})))
const app = new Koa()
app.use(router.routes())
app.listen(3000)
// localhost:3000/jokes -> http://api.icndb.com/jokes
// localhost:3000/jokes/random -> http://api.icndb.com/jokes/random
Let me know if this works and I'll it to the list.
Hi, @chimurai , Thanks to the awesome work. I try it, I can't work when I have url path.
const Koa = require('koa');
const Router = require('koa-router');
const proxy = require('http-proxy-middleware');
const c2k = require('koa2-connect');
const app = new Koa();
const router = new Router();
router.get(
'/',
c2k(
proxy({
target: 'https://api.github.com/',
changeOrigin: true
})
)
);
app.use(router.routes());
app.listen(3000);
When I access the localhost:3000, and it can return the https://api.github.com, but
when I access the localhost:3000/users/monkindey/orgs, the expected result is
return the https://api.github.com/users/monkindey/orgs, but it return the Not found.
I try it with express and it can work well.
Sorry about that, I figure out the reason, I can fix it with changing the router match.
router.get(
'/',
c2k(
proxy({
target: 'https://api.github.com/',
changeOrigin: true
})
)
);
router.get(
'*',
c2k(
proxy({
target: 'https://api.github.com/',
changeOrigin: true
})
)
);
router.get('/', ...) will only route the root path.
Please check the koa-router documentation for more configuration options: https://github.com/alexmingoia/koa-router#new-routeropts
Thanks for your information
It does not work for me, the form data is not properly passed, there are some other strange performance.
@chimurai webpack-dev-middleware added support by wrapping with Promises. The changes were relatively trivial. see: https://github.com/webpack/webpack-dev-middleware/commit/26f0b4028b15cb0e12916e1e34eac95a97ed7b8b
Something you'd consider adding?
welcome to use koa2-proxy-middleware
Not recommended with router. you need koa2-connect. Usage is as follows:
const proxy = require('http-proxy-middleware');
const k2c = require('koa2-connect');
var options = {
target: config.url, // target host
changeOrigin: true, // needed for virtual hosted sites
ws: true, // proxy websockets
pathRewrite: {
'^/api' : '/api', // rewrite path
}
};
app.use(k2c(proxy(options)));
Its work.
i can proxy my address from a target,but how to fix the http status 302? it redirect to the target,but not mine
I found this paradigm to work best instead of using router, which interfered with Next.js: https://github.com/chimurai/http-proxy-middleware/issues/297#issuecomment-422284087
For quick preview, here's what mine looks like:
app.use(c2k(proxy('/api', { target: API_URL })));
Edit: I didn't see that davecat had sampled this same idea already in this thread.
@shellscape Thanks for suggestion. (https://github.com/chimurai/http-proxy-middleware/issues/155#issuecomment-354121546)
async/Promise middleware will land in next version.
update: landed in v0.20.0
https://github.com/chimurai/http-proxy-middleware/releases/tag/v0.20.0
我试一下评论之后是咋样的
welcome to use koa2-nginx. 💯
Or koa-http2-proxy for that matter ;)
Most helpful comment
@chimurai webpack-dev-middleware added support by wrapping with
Promises. The changes were relatively trivial. see: https://github.com/webpack/webpack-dev-middleware/commit/26f0b4028b15cb0e12916e1e34eac95a97ed7b8bSomething you'd consider adding?