http-proxy-middleware icon indicating copy to clipboard operation
http-proxy-middleware copied to clipboard

How shall I use it in Koa2.0 ?

Open fengxinming opened this issue 9 years ago • 15 comments

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 avatar Mar 20 '17 05:03 fengxinming

@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.

chimurai avatar Apr 30 '17 13:04 chimurai

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.

monkindey avatar May 30 '17 03:05 monkindey

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
    })
  )
);

monkindey avatar May 30 '17 03:05 monkindey

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

chimurai avatar May 30 '17 11:05 chimurai

Thanks for your information

monkindey avatar May 30 '17 14:05 monkindey

It does not work for me, the form data is not properly passed, there are some other strange performance.

yoyo837 avatar Sep 15 '17 03:09 yoyo837

@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?

shellscape avatar Dec 27 '17 14:12 shellscape

welcome to use koa2-proxy-middleware

sunyongjian avatar May 25 '18 11:05 sunyongjian

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.

davecat avatar Sep 10 '18 04:09 davecat

i can proxy my address from a target,but how to fix the http status 302? it redirect to the target,but not mine

brucemeteor avatar Oct 24 '18 08:10 brucemeteor

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.

juliantrueflynn avatar Jan 31 '19 01:01 juliantrueflynn

@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

chimurai avatar Apr 04 '19 20:04 chimurai

我试一下评论之后是咋样的

douyacun avatar Aug 21 '19 15:08 douyacun

welcome to use koa2-nginx. 💯

my9074 avatar Nov 25 '19 11:11 my9074

Or koa-http2-proxy for that matter ;)

arthurWD avatar Nov 25 '19 12:11 arthurWD