question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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

See original GitHub issue

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 https://github.com/chimurai/http-proxy-middleware/pull/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).

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
dtochcommented, Apr 16, 2021

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

1reaction
dtochcommented, Apr 12, 2021

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

Read more comments on GitHub >

github_iconTop Results From Across the Web

Routing error in dev mode: TypeError: Cannot read property ...
5.2 fixes the issue. Launch dev server; Load any URL; Click any internal link. Unhandled Runtime Error TypeError: Cannot read property 'includes ...
Read more >
Node.js TypeError: Cannot read property 'host' of undefined
I think the problem is that you have a typo in config.js . You are using module.export instead of module.exports , so you're...
Read more >
Cannot read property 'host' of undefined - WordPress.org
Hi,. I have setup your plugin and added all details up to date. but on my checkout page plugin show Cannot read property...
Read more >
Cannot read property 'url' of undefined when starting server.js ...
Getting this error when running server.js. Started http server. info - socket.io started node.js:201 throw e; // process.
Read more >
Getting error "Cannot read property 'url' of undefined" when ...
Coding example for the question Getting error "Cannot read property 'url' of undefined" when data is present-Vue.js.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found