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

Example code does not work as intended

Open caritasverein opened this issue 4 years ago • 4 comments

The example code does not work as intended.

Steps to reproduce

Use the example code:

var express = require('express');
var proxy = require('http-proxy-middleware');

var app = express();

app.use(
  '/api',
  proxy({ target: 'http://www.example.org', changeOrigin: true })
);
app.listen(3000);

Expected behavior

This should create a proxy for /api to http://www.example.org/api. The debug output should look like this:

[HPM] Proxy created: /api -> http://www.example.org

Actual behavior

It creates a proxy for / to http://www.example.org. The debug output looks like this:

[HPM] Proxy created: / -> http://www.example.org

Workaround

As provided in the core concept in the README.md, move the /api from app.use() to proxy(). Like this:

app.use(
  proxy('/api', { target: 'http://www.example.org', changeOrigin: true })
);

This example works as intended, the debug output looks like this:

[HPM] Proxy created: /api -> http://www.example.org

Setup

This happens in

[email protected] [email protected] node v10.15.3 Windows 10 1903 (Build 18362.418).

I did not test this with other combinations yet. I hope i did not misunderstood anything. If so, feel free to tell me and close the issue :-). Thank you for this project!

caritasverein avatar Jan 03 '20 07:01 caritasverein

I ran into the same issue... dont know when this changed honestly, but im concerned it may add overhead since it skips the express pattern matching layer... is there a good workaround?

chaffeqa avatar Jan 06 '20 13:01 chaffeqa

same problem here, using the latest version of this library, used node v14.x.x and followed the example code in README. getting the same result where proxying / instead of the path I am using in app.use

pencilcheck avatar Sep 14 '22 05:09 pencilcheck

+1 on this. Is the solution provided by @caritasverein the proper one?

vincepmartin avatar Mar 14 '23 18:03 vincepmartin

Please have a look at http-proxy-middleware@beta (v3.0.0-beta.1)

Logging has been improved and hopefully less confusing.

Happy to hear your feedback

chimurai avatar Mar 14 '23 19:03 chimurai