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.

express.use(path, proxy()) ignores path for websockets

See original GitHub issue

Also 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:open
  • Created 6 years ago
  • Comments:9 (1 by maintainers)

github_iconTop GitHub Comments

7reactions
roccomusocommented, Sep 27, 2017

I’m having the same issue… I kinda fixed it passing the “context” twice:

express.use('/__webpack_hmr', proxy('/__webpack_hmr', { ws: true, target: 'http://...' }))

@ftes give it a try.

3reactions
kdemarestcommented, May 14, 2019

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:

this.socket = io.connect('http://localhost:3000/shadowStone');

when I should have been starting it with:

this.socket = io.connect('http://localhost:3000', {'path': '/shadowStone/socket.io'});

and the server-side companion code to this is

app.use( '/shadowStone', Express.static('.') )
let server = app.listen(3000)
io = SocketIo(server, {path:'/shadowStone/socket.io'});

In my proxy, the code is quite simple:

let wsProxy = Proxy({
	target: 'ws://localhost:3000',
	logLevel: 'debug',
});

app.use( '/shadowStone', wsProxy );
let server = app.listen(port);
server.on('upgrade', wsProxy.upgrade)

Hope this helps somebody with a similar problem…

Read more comments on GitHub >

github_iconTop 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 >

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