Skip to content

Error when running example #112

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
twobitfool opened this issue Sep 21, 2011 · 16 comments
Closed

Error when running example #112

twobitfool opened this issue Sep 21, 2011 · 16 comments

Comments

@twobitfool
Copy link

The Proxy requests within another http server example (from README.md) raises the following error:

Both `options` and `options.target` are required.
@AvianFlu
Copy link
Contributor

There has been an API change, and the examples do not yet reflect it. Passing a target object in the form of { host: host, port: port } to the constructor will solve this issue.

Sorry about the confusion - I'll try to get the docs updated ASAP.

@coolaj86
Copy link

Same here.

Example:

(function () {
  "use strict";

  var connect = require('connect')
    , httpProxy = require('http-proxy')
    , proxy
    , server
    ;

  function doProxy(req, res, next) {
    // forward to a development rails app
    proxy.proxyRequest(req, res, {
      host: 'localhost',
      port: 3000
    });
  }

  //
  // Create a new instance of HttProxy to use in your server
  //
  proxy = new httpProxy.HttpProxy();
  /*
  // this doesn't work either
  proxy = new httpProxy.HttpProxy({
      host: 'localhost'
    , port: 3000
    , target: {}
  });
  */

  server = connect.createServer(doProxy);

  module.exports = server;
}());

@AvianFlu
Copy link
Contributor

See my above comment - target has host and port within it, they are not parallel arguments.

@coolaj86
Copy link

This is still a no-go for me.

TypeError: Object #<Object> has no method 'resume'
    at [object Object].proxyRequest (/var/webapps/vhosts/http-proxy-test/node_modules/http-proxy/lib/node-http-proxy/http-proxy.js:323:16)
    at Object.doProxy [as handle] (/var/webapps/vhosts/http-proxy-test/server.js:11:11)
    at next (/var/webapps/node_modules/connect/lib/http.js:204:15)
    at HTTPServer.handle (/var/webapps/node_modules/connect/lib/http.js:217:3)
    at HTTPServer.emit (events.js:67:17)
    at HTTPParser.onIncoming (http.js:1134:12)
    at HTTPParser.onHeadersComplete (http.js:108:31)
    at Socket.ondata (http.js:1029:22)
    at Socket._onReadable (net.js:681:27)
    at IOWatcher.onReadable [as callback] (net.js:177:10)

server.js:

(function () {
  "use strict";

  var connect = require('connect')
    , httpProxy = require('http-proxy')
    , proxy
    , server
    ;

  function doProxy(req, res, next) {
    proxy.proxyRequest(req, res, {
        target: {
            host: 'localhost'
          , port: 3000
        }
    });
  }

  //
  // Create a new instance of HttProxy to use in your server
  //
  proxy = new (httpProxy.HttpProxy)({
      target: {
          host: 'localhost'
        , port: 3000
      }
  });

  server = connect.createServer(doProxy);

  module.exports = server;
}());

@AvianFlu
Copy link
Contributor

Connect is probably buffering the request, in which case it isn't a stream anymore, and no longer has a resume method. Here is an example without Connect. https://gist.github.com/1232446

Does that help you at all?

@indexzero
Copy link
Member

There indeed has been an API change which is more significant: if you were using the HttpProxy before that is now a RoutingProxy:

(function () {
  "use strict";

  var connect = require('connect')
    , httpProxy = require('http-proxy')
    , proxy
    , server
    ;

  function doProxy(req, res, next) {
    proxy.proxyRequest(req, res, {
        host: 'localhost'
      , port: 3000
    });
  }

  //
  // Create a new instance of RoutingProxy to use in your server
  //
  proxy = new (httpProxy.RoutingProxy)();

  server = connect.createServer(doProxy);

  module.exports = server;
}());

Conversely, if you want to use the old HttpProxy object, you do not pass anything to .proxyRequest():

(function () {
  "use strict";

  var connect = require('connect')
    , httpProxy = require('http-proxy')
    , proxy
    , server
    ;

  function doProxy(req, res, next) {
    proxy.proxyRequest(req, res);
  }

  //
  // Create a new instance of HttpProxy to use in your server
  //
  proxy = new (httpProxy.HttpProxy)({
      target: {
          host: 'localhost'
        , port: 3000
      }
  });

  server = connect.createServer(doProxy);

  module.exports = server;
}());

What examples are broken? We can update them accordingly.

@cararemixed
Copy link

Charlie, I think the READMe is out of date IIRC.

@AvianFlu
Copy link
Contributor

I'm fixing the README and all the examples now. I'll publish the changes as soon as I'm sure all the examples work.

@manast
Copy link

manast commented Sep 29, 2011

Hmm, This is still not working for me. Documents are outdated or there is in fact a bug when using middleware. I get the following error:

/node_modules/http-proxy/lib/node-http-proxy/routing-proxy.js:189
  proxy.proxyRequest(req, res, options.buffer);



        ^
TypeError: Cannot call method 'proxyRequest' of undefined
    at [object Object].proxyRequest (/Users/manuel/dev/clouddisplay/node_modules/http-proxy/lib/node-http-proxy/routing-proxy.js:189:9)
    at Array.0 (/Users/manuel/dev/clouddisplay/proxy.js:71:22)
    at Server.<anonymous> (/Users/manuel/dev/clouddisplay/node_modules/http-proxy/lib/node-http-proxy.js:173:39)
    at Server.emit (events.js:67:17)
    at HTTPParser.onIncoming (http.js:1131:12)
    at HTTPParser.onHeadersComplete (http.js:108:31)
    at Socket.ondata (http.js:1026:22)
    at Socket._onReadable (net.js:683:27)
    at IOWatcher.onReadable [as callback] (net.js:177:10)

@manast
Copy link

manast commented Sep 29, 2011

well, forget about the previous error. It was created by me when trying to fix the real problem I found. Namely that socket.io related requests do seem to use the new API while normal http request do not. For instance, I have a simple proxy created using:

var proxyServer = httpProxy.createServer(myMiddleware)
proxyServer.listen(config.proxyPort)

Now, inside my middleware I do this call:
proxy.proxyRequest(req,res, dest);

where dest is in the form {port:8081, host:'myhost'}

This works well until socket.io tries to make a request, in that case I get the following error:
node_modules/http-proxy/lib/node-http-proxy/routing-proxy.js:239
throw new Error('options.host and options.port or options.target are requi
^
Error: options.host and options.port or options.target are required.
at [object Object]._getKey (/Users/manuel/dev/clouddisplay/node_modules/http-proxy/lib/node-http-proxy/routing-proxy.js:239:11)
at [object Object].add (/Users/manuel/dev/clouddisplay/node_modules/http-proxy/lib/node-http-proxy/routing-proxy.js:70:18)
at [object Object].proxyWebSocketRequest (/Users/manuel/dev/clouddisplay/node_modules/http-proxy/lib/node-http-proxy/routing-proxy.js:222:10)
at Server. (/Users/manuel/dev/clouddisplay/node_modules/http-proxy/lib/node-http-proxy.js:190:13)
at Server.emit (events.js:81:20)
at Socket. (http.js:1042:14)
at Socket._onReadable (net.js:683:27)
at IOWatcher.onReadable as callback

Seems to me like there is some inconsistence with the APIs right now :/

@indexzero
Copy link
Member

@manast I need a bigger code sample to understand what you are referring to. If you are trying to proxy WebSockets, why are you not calling .proxyWebSocketRequest()?

@manast
Copy link

manast commented Sep 30, 2011

Hello,

I will update with more code. But the reason I am not using proxyWebsocketRequest is because I did not need to do it before, and it worked well. Its after the API change that this has stop working.

regards.

On Sep 29, 2011, at 10:22 PM, Charlie Robbins wrote:

@manast I need a bigger code sample to understand what you are referring to. If you are trying to proxy WebSockets, why are you not calling .proxyWebSocketRequest()?

Reply to this email directly or view it on GitHub:
#112 (comment)

@indexzero
Copy link
Member

@manast I need a full code sample to give you any help.

@manast
Copy link

manast commented Sep 30, 2011

Ok, here it comes :)

function matcher (url, dest) {
  var r = new RegExp (url);

  return function (url) {
    var m = r(url);
    if (!m) return
      var path = url.slice(m[0].length);
      logger.debug('URL match: ' + url + ' -> '+ path);
      return {
        url: path,
        dest: dest
      }
  }
}

exports.urls = function (urls) {
  var matchers = [];
  for (var url in urls) {
    matchers.push(matcher(url, urls[url]));
  }

  return function (req, res, next) {
    var proxy = next;

    for (var k=0; k<matchers.length;k++) {
      var m
      if (m = matchers[k](req.url)) {
        req.url = m.url;
        return proxy.proxyRequest(req, res, m.dest);
      }
    }
  }
}

var proxyServer =  httpProxy.createServer(
  exports.urls({
    '/api': {port: 8081, host: 'localhost'},
    '/pubsub':{port: 8083, host: 'localhost'},
    '/rekon': {port: 8098, host: 'localhost'},
    '': {port: 3000, host: 'localhost'},
  })
)
proxyServer.listen(80);

@indexzero indexzero reopened this Sep 30, 2011
@manast
Copy link

manast commented Oct 11, 2011

@indexzero

any news on this issue? :)

@cronopio
Copy link
Contributor

cronopio commented Jun 5, 2012

New version of node-http-proxy was released. 0.8.1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

7 participants