question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Serves as a proxy using node-http-proxy

See original GitHub issue

Hi 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:closed
  • Created 10 years ago
  • Comments:5 (4 by maintainers)

github_iconTop GitHub Comments

5reactions
jonathanongcommented, Jan 24, 2014

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:

var app = koa()
var callback = app.callback()

http.createServer(function (req, res) {
  var path = url.parse(req.url).pathname
  if (/^\/proxy\/.*/.test(path)) return proxy.web(req, res, {proxy: '' })
  callback(req, res)
})

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

0reactions
jonathanongcommented, Jan 24, 2014

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

Read more comments on GitHub >

github_iconTop Results From Across the Web

How can I use an http proxy with node.js http.Client?
Using a HTTP proxy (for non secure requests) is very simple. You connect to the proxy and make the request normally except that...
Read more >
Build a Node.js Proxy Server in Under 10 minutes! - Twilio
In a few easy steps we are going to create a simple proxy in Node.js which can forward requests to multiple different servers/endpoints!...
Read more >
A full-featured http proxy for node.js - GitHub
node -http-proxy is an HTTP programmable proxying library that supports websockets. It is suitable for implementing components such as reverse proxies and ...
Read more >
Node.js - Simple Proxy to Pass Through HTTP Requests to an ...
This is a quick example of how to proxy an HTTP request through a Node.js server to an external URL and return the...
Read more >
What is a proxy, and how does it work in Node.js?
A reverse proxy receives every request from the client, acting as an intermediary server, then forwards these requests to the actual server(s) ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found