http-route-proxy icon indicating copy to clipboard operation
http-route-proxy copied to clipboard

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

Open adamzerner opened this issue 10 years ago • 5 comments

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 avatar Aug 25 '15 21:08 adamzerner

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 [email protected]

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'
}));

switer avatar Aug 26 '15 03:08 switer

I tried a) uninstalling and reinstalling and b) installing [email protected] like you recommended. Neither worked. Also, it looks like the latest version is 0.10.4, not 0.2.0.

└── [email protected] ([email protected], [email protected], [email protected])

Here's my output:

~/code/fire_starter_node $ npm uninstall http-route-proxy
unbuild [email protected]
~/code/fire_starter_node $ npm install http-route-proxy --save
npm WARN package.json [email protected] No description
npm WARN package.json [email protected] No repository field.
npm WARN package.json [email protected] No README data
[email protected] node_modules/http-route-proxy
├── [email protected]
├── [email protected]
├── [email protected] ([email protected], [email protected], [email protected], [email protected])
└── [email protected] ([email protected], [email protected], [email protected])
~/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 [email protected]
~/code/fire_starter_node $ npm i [email protected] --save
npm WARN package.json [email protected] No description
npm WARN package.json [email protected] No repository field.
npm WARN package.json [email protected] No README data
[email protected] node_modules/http-route-proxy
├── [email protected]
├── [email protected]
├── [email protected] ([email protected], [email protected], [email protected], [email protected])
└── [email protected] ([email protected], [email protected], [email protected])
~/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 avatar Aug 26 '15 14:08 adamzerner

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

adamzerner avatar Aug 26 '15 14:08 adamzerner

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.

switer avatar Aug 26 '15 14:08 switer

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!

adamzerner avatar Aug 26 '15 16:08 adamzerner