[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 issueIs 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 :
- npm install
- node test.js
- curl http://localhost:3000/test
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:
- Created 2 years ago
- Comments:5 (3 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Thanks. I confirm the issue doesn’t appear with the new stable release http-proxy-middleware@1.1.2.
I confirm the issue doesn’t appear with http-proxy-middleware@1.1.2-alpha.1.