Silly WebSocket mistake

I recently was trying to add a websocket to an existing NodeJS Express app I had written. Here is the important code below:

 

const WebSocket = require('ws');
const app = express();
const server = http.createServer(app);
const wss = new WebSocket.Server({ server });

app.listen(8080, function listening() {
  console.log('Listening on %d', server.address().port);
})


I kept getting 502 errors from NGINX with this code when I tried to establish a connection. See if you can spot the error…

I was pretty dumb. You can’t have app listening anymore. Instead you have to have server. The correct code is:

server.listen(8080, function listening() {
  console.log('Listening on %d', server.address().port);
});

Leave a comment if this happened to you too… I will feel less dumb.


Posted

in

by

Tags: