Skip to content
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

Request for more documentation + question on why /api is being stripped off. #1

Open
adamzerner opened this issue Aug 25, 2015 · 5 comments

Comments

@adamzerner
Copy link

I'd find more documentation useful. I like that common scenarios are covered with good examples, but I don't see how to branch out from there.

For example, I'm trying to route everything prefaced with /api to localhost:3001, and everything else to localhost:3002 but can't figure out how to do so.

When I do this:

var express = require('express');
var proxyServer = require('http-route-proxy');
var app = express();

app.use('/api', proxyServer.connect({
  from: 'localhost:3000',
  to: 'localhost:3001', // api server
  https: false,
  route: ['/']
}));

app.use(proxyServer.connect({
  from: 'localhost:3000',
  to: 'localhost:3002', // web server
  https: false,
  route: ['/']
}));

app.listen(3000, function() {
  console.log('Proxy listening on port 3000...');
});

The /api part gets stripped off somehow:

forward GET  /auth/facebook from localhost:3000 to http://localhost:3001/auth/facebook
@adamzerner adamzerner changed the title Request: more documentation Request for more documentation + question on why /api is being stripped off. Aug 25, 2015
@switer
Copy link
Owner

switer commented Aug 26, 2015

The /api part gets stripped off somehow:

It's a bug. http-route-proxy using req.url as proxy route url, so it will be stripped off by express.

I had fixed it, please update with v0.2.0:

npm i http-route-proxy@0.2.0

https default is false, and route is optional, so the config can be more simple:

app.use('/api', proxyServer.connect({
  from: 'localhost:3000',
  to: 'localhost:3001'
}));

@adamzerner
Copy link
Author

I tried a) uninstalling and reinstalling and b) installing http-route-proxy@0.2.0 like you recommended. Neither worked. Also, it looks like the latest version is 0.10.4, not 0.2.0.

└── http-proxy@0.10.4 (pkginfo@0.3.0, optimist@0.6.1, utile@0.2.1)

Here's my output:

~/code/fire_starter_node $ npm uninstall http-route-proxy
unbuild http-route-proxy@0.1.1
~/code/fire_starter_node $ npm install http-route-proxy --save
npm WARN package.json fire_starter_node@1.0.0 No description
npm WARN package.json fire_starter_node@1.0.0 No repository field.
npm WARN package.json fire_starter_node@1.0.0 No README data
http-route-proxy@0.1.1 node_modules/http-route-proxy
├── colors@0.6.2
├── underscore@1.5.2
├── send@0.1.4 (range-parser@0.0.4, fresh@0.2.0, mime@1.2.11, debug@2.2.0)
└── http-proxy@0.10.4 (pkginfo@0.3.0, optimist@0.6.1, utile@0.2.1)
~/code/fire_starter_node $ node proxy.js
Proxy listening on port 3000...
forward GET  / from localhost:3000 to http://localhost:3002/
forward GET  /favicon.ico from localhost:3000 to http://localhost:3002/favicon.ico
forward GET  /auth/facebook from localhost:3000 to http://localhost:3001/auth/facebook
forward GET  /favicon.ico from localhost:3000 to http://localhost:3002/favicon.ico
^C
~/code/fire_starter_node $ npm uninstall http-route-proxy --save
unbuild http-route-proxy@0.1.1
~/code/fire_starter_node $ npm i http-route-proxy@0.2.0 --save
npm WARN package.json fire_starter_node@1.0.0 No description
npm WARN package.json fire_starter_node@1.0.0 No repository field.
npm WARN package.json fire_starter_node@1.0.0 No README data
http-route-proxy@0.2.0 node_modules/http-route-proxy
├── colors@0.6.2
├── underscore@1.5.2
├── send@0.1.4 (range-parser@0.0.4, fresh@0.2.0, mime@1.2.11, debug@2.2.0)
└── http-proxy@0.10.4 (pkginfo@0.3.0, optimist@0.6.1, utile@0.2.1)
~/code/fire_starter_node $ node proxy.js
Proxy listening on port 3000...
forward GET  / from localhost:3000 to http://localhost:3002/
forward GET  /favicon.ico from localhost:3000 to http://localhost:3002/favicon.ico
forward GET  /api/auth/facebook from localhost:3000 to http://localhost:3001/api/auth/facebook

@adamzerner
Copy link
Author

When I use remove route:

app.use('/api', proxyServer.connect({
  from: 'localhost:3000',
  to: 'localhost:3001' // api server
}));

app.use(proxyServer.connect({
  from: 'localhost:3000',
  to: 'localhost:3002', // web server
}));

Proxy says that it's working:

forward GET  /api/auth/facebook from localhost:3000 to http://localhost:3001/api/auth/facebook

But when I log req.url, it says /auth/facebook:

app.use('/', function(req, res, next) {
  console.log('hits here');
  console.log('req: ', req.url);
  next();
});

Output:

hits here
req:  /auth/facebook

@switer
Copy link
Owner

switer commented Aug 26, 2015

http-proxy is a dependency. http-route-proxy latest version is 0.2.0.

But when I log req.url, it says /auth/facebook:

It's my mistake, i should rewrite req.url and req.path otherwise proxy-agent will use the req object that was modified by express directly. It will be fixed at v0.2.1. : )


v0.2.1 is published, using npm i http-route-proxy@latest to update.

@adamzerner
Copy link
Author

http-proxy is a dependency. http-route-proxy latest version is 0.2.0.

Oh, whoops - I misread.

v0.2.1 is published, using npm i http-route-proxy@latest to update.

Works great - thanks!

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

No branches or pull requests

2 participants