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

[BUG] http-proxy-middleware@1.1.1 : "Cannot read property 'host' of undefined" if target not set and router returns unresolvable URL. #521

Closed
dtoch opened this issue Apr 12, 2021 · 5 comments
Labels

Comments

@dtoch
Copy link

dtoch commented Apr 12, 2021

Is this a bug report?

YES

Steps to reproduce

  • package.json :
{
  "name": "testbug",
  "version": "0.0.0",
  "dependencies": {
    "express": "4.17.1",
    "http-proxy-middleware": "1.1.1"
  }
}
  • test.js
const express = require('express')

const app = express()

const { createProxyMiddleware  } = require('http-proxy-middleware');

const options = {
    router: function (req) {
       return "http://INVALID";
    }
};

app.use('/test', createProxyMiddleware (options));

app.listen(3000, () => {
  console.log(`Example app listening at http://localhost:3000`)
})

Then :

Result : following runtime error is logged :

TypeError: Cannot read property 'host' of undefined
    at ProxyServer.HttpProxyMiddleware.logError (C:\Users\dtoch\work\projects\pga\pga-iaf\iaf-hil-common-server-node\node_modules\.pnpm\http-proxy-middleware@1.1.1_debug@4.3.1\node_modules\http-proxy-middleware\dist\http-proxy-middleware.js:141:87)
    at ProxyServer.emit (C:\Users\dtoch\work\projects\pga\pga-iaf\iaf-hil-common-server-node\node_modules\.pnpm\eventemitter3@4.0.7\node_modules\eventemitter3\index.js:210:27)
    at ClientRequest.proxyError (C:\Users\dtoch\work\projects\pga\pga-iaf\iaf-hil-common-server-node\node_modules\.pnpm\http-proxy@1.18.1_debug@4.3.1\node_modules\http-proxy\lib\http-proxy\passes\web-incoming.js:165:18)

Expected behavior

Not a TypeError runtime error should be generated but instead a normal error just indicating that the backend URL is invalid.

Actual behavior

TypeError: Cannot read property 'host' of undefined

Workaround

According to #512 part of http-proxy-middleware@1.1.1 it should not be needed to define target if using router. However to avoid above error, it is still needed to set it to something e.g. XXX.

Setup

See above.

client info

  • node 12.20.2
  • npm 7.9.0

target server info

target not defined (only using router section in options).

@chimurai
Copy link
Owner

Thanks for reporting.
Think proposed solution in #242 might fix the issue

@chimurai
Copy link
Owner

Could you try and verify if http-proxy-middleware@1.1.2-alpha.1 fixes the issue?

@chimurai chimurai added the bug label Apr 12, 2021
@dtoch
Copy link
Author

dtoch commented Apr 12, 2021

I confirm the issue doesn't appear with http-proxy-middleware@1.1.2-alpha.1.

@chimurai
Copy link
Owner

chimurai commented Apr 14, 2021

Published v1.1.2

Implementation/fix in final v1.1.2 is different than the previous http-proxy-middleware@1.1.2-alpha.1.

Please let me know if run into issues.

@dtoch
Copy link
Author

dtoch commented Apr 16, 2021

Thanks. I confirm the issue doesn't appear with the new stable release http-proxy-middleware@1.1.2.

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

No branches or pull requests

2 participants