express.use(path, proxy()) ignores path for websockets
See original GitHub issueAlso see expressjs/express#3428.
Expected behavior
The following should listen for websocket requests only under /__webpack_hmr
.
express.use('/__webpack_hmr', proxy({ ws: true, target: 'http://...' }));
Websocket requests to other paths (e.g. /socket.io
) should not be handled by the proxy.
Actual behavior
The proxy “hijacks” all incoming websocket requests, also those to /socket.io
(see socketio/socket.io#3062).
Setup
Fiddle: https://github.com/ftes/socket.io-fiddle/pull/1/files
- http-proxy-middleware: 0.17.4
- server: express 4.13.4
- socket.io: 2.0.3
proxy middleware configuration
proxy({ ws: true, target: 'http://localhost:4000' });
server mounting
var app = express();
app.use('/__webpack_hmr', proxy);
app.listen(3000);
Workaround
Pass path /__webpack_hmr
as context to proxy.
const webpackHmrProxy = proxy('/__webpack_hmr', {
target: 'http://localhost:4000',
ws: true,
});
app.use(webpackHmrProxy);
Issue Analytics
- State:
- Created 6 years ago
- Comments:9 (1 by maintainers)
Top Results From Across the Web
node.js - Proxy with express.js - Stack Overflow
request to create a similar request to the remote API and return its response. Something like this: const http = require('http'); // or...
Read more >http-proxy-middleware - npm
use path parameter to match requests; Create and mount the proxy without the http-proxy-middleware context parameter: app.use('/api', ...
Read more >Server Options - Vite
Configure custom proxy rules for the dev server. Expects an object of { key: options } pairs. Any requests that request path starts...
Read more >Behind a reverse proxy | Socket.IO
IO server behind a reverse-proxy solution, such as: ... NginX; Apache HTTPD; Node.js http-proxy; Caddy 2 ... Or with a custom path:.
Read more >Web on Servlet Stack - Spring
From these inputs, Spring MVC needs to determine the lookup path to use for ... In some cases, you may need to decorate...
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
I’m having the same issue… I kinda fixed it passing the “context” twice:
@ftes give it a try.
I found my issue. I was using socket.io, which is very particular about how you initialize it. In the target of the proxy, in index.html, I was starting socket.io with:
when I should have been starting it with:
and the server-side companion code to this is
In my proxy, the code is quite simple:
Hope this helps somebody with a similar problem…