Serves as a proxy using node-http-proxy
See original GitHub issueHi there,
I am new to koa/node.js so please forgive my ignorance if this looks like a basic question to you.
I am looking into writing a proxy server to one of my HTTP server. I would like to use a library like https://github.com/nodejitsu/node-http-proxy and koa/koa-router to do this.
I basically followed the documentation available in node-http-proxy/koa/koa-router to write some code snippet like this:
var koa = require('koa');
var router = require('koa-router');
var app = koa();
app.use(router(app));
...
app.get('/proxy/*', function *(next) {
proxy.web(this.req, this.res, { target: 'http://www.google.com' });
});
But I got some error like
_http_outgoing.js:331
throw new Error('Can\'t set headers after they are sent.');
^
Error: Can't set headers after they are sent.
at ServerResponse.OutgoingMessage.setHeader (_http_outgoing.js:331:11)
at .../node_modules/http-proxy/lib/http-proxy/passes/web-outgoing.js:58:11
I went through koa wiki pages/examples but I am not really sure what goes wrong here. I did find several similar koa issues logged, including https://github.com/koajs/koa/issues/95, https://github.com/koajs/koa/pull/139, and https://github.com/koajs/koa/issues/142, but I didn’t have any idea how I can address my problem here after reviewing all these. Could you please help to shed some light on this? Thanks so much.
Issue Analytics
- State:
- Created 10 years ago
- Comments:5 (4 by maintainers)
Top GitHub Comments
you don’t need to shoehorn into koa. koa makes some opinions, and unfortunately i haven’t found a way to make it compatible with the regular
req, res, next
signature.just use it outside of express:
@visionmedia do you want to add a
this.response = false
flag so people can skip koa’s default response handling? it would avoid support issues, but i think 99% of the time they are doing it wrong if they need to do that.yeah you can mount koa as a connect/express app, which is pretty much what my example above is. you just can’t pass a callback to
app.callback()
though since it won’t work like a connect/express app if we supported that (unless we executed the callback only on errors, but that’s weird).