Skip to content

[BUG] [email protected] : "Cannot read property 'host' of undefined" if target not set and router returns unresolvable URL. #521

@dtoch

Description

@dtoch

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions