How shall I use it in Koa2.0 ? #554
Replies: 15 comments
-
Found a working setup using Setup:
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. |
Beta Was this translation helpful? Give feedback.
-
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 |
Beta Was this translation helpful? Give feedback.
-
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
})
)
); |
Beta Was this translation helpful? Give feedback.
-
Please check the koa-router documentation for more configuration options: https://github.com/alexmingoia/koa-router#new-routeropts |
Beta Was this translation helpful? Give feedback.
-
Thanks for your information |
Beta Was this translation helpful? Give feedback.
-
It does not work for me, the form data is not properly passed, there are some other strange performance. |
Beta Was this translation helpful? Give feedback.
-
@chimurai webpack-dev-middleware added support by wrapping with Something you'd consider adding? |
Beta Was this translation helpful? Give feedback.
-
welcome to use koa2-proxy-middleware |
Beta Was this translation helpful? Give feedback.
-
Not recommended with router. you need koa2-connect. Usage is as follows: Its work. |
Beta Was this translation helpful? Give feedback.
-
i can proxy my address from a target,but how to fix the http status 302? it redirect to the target,but not mine |
Beta Was this translation helpful? Give feedback.
-
I found this paradigm to work best instead of using router, which interfered with Next.js: #297 (comment) For quick preview, here's what mine looks like:
Edit: I didn't see that davecat had sampled this same idea already in this thread. |
Beta Was this translation helpful? Give feedback.
-
@shellscape Thanks for suggestion. (#155 (comment)) async/Promise middleware will land in next version. update: landed in v0.20.0 |
Beta Was this translation helpful? Give feedback.
-
我试一下评论之后是咋样的 |
Beta Was this translation helpful? Give feedback.
-
welcome to use koa2-nginx. 💯 |
Beta Was this translation helpful? Give feedback.
-
Or koa-http2-proxy for that matter ;) |
Beta Was this translation helpful? Give feedback.
-
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 !
Beta Was this translation helpful? Give feedback.
All reactions