title: 404 error with express and express-ws
body: <p>I'm having trouble trying to figure out why I'm getting this 404 error. I've gone through all the other questions on this site that cover 'express-ws' and i've modeled my code exactly how the solutions prescribed yet the websocket won't make a connection. I'm trying to create a websocket connection between my express server and react app. Below are previews of my code:</p>
  
  <p>Express using express-ws (server.js):</p>
  
  <pre><code>var express = require('express');
  var expressWs = require('express-ws');
  var bodyParser = require('body-parser');
  var cors = require('cors');
  var nodemailer = require('nodemailer');
  var email = require('./credentials');
  var port = process.env.PORT || 3001;
  var path = require('path');
  // const WebSocket = require('ws');
  // const http = require('http');
  
  expressWs = expressWs(express());
  
  let app = expressWs.app;
  
  var server = require('http').createServer(app);
  
  app.use(cors());
  app.use(bodyParser.json());
  app.use(bodyParser.urlencoded({ extended: true }));
  
  if (process.env.NODE_ENV === 'production') {
    app.use(express.static('app/build'));
  }
  
  app.get('/', function(req, res){
    console.log('server running!');
    res.end();
  });
  
  app.ws('/ws', function(ws, req) {
    console.log( 'socket running!' );
  });
  
  server.listen(port);
  
  console.log('server started on port ' + port);
  </code></pre>
  
  <p>The GET route works fine but the ws route doesn't.</p>
  
  <p>Call to express from React app:</p>
  
  <pre><code>componentDidMount() {
    let ws = new WebSocket('ws://localhost:3001/ws');
  
    ws.on( 'open', function open() {
      console.log('app connected to websocket!');
    } );
  
    ws.on( 'message', function ( message ) {
      console.log( message );
    })
  }
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/tL1Gv.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/tL1Gv.png" alt="Console log of GET error from ws call:"></a></p>
  
  <p>I've looked at all the following questions and don't understand why their solutions don't work for me:</p>
  
  <ul>
  <li><p><a href="https://stackoverflow.com/questions/40795708/socket-io-returns-404-error-with-express-http">Socket.IO 404 Error</a></p></li>
  <li><p><a href="https://stackoverflow.com/questions/40438048/cant-create-a-websocket-sever-correctly-by-express-ws-middleware">express-ws connection problem</a></p></li>
  <li><p><a href="https://stackoverflow.com/questions/40764640/node-server-not-working-with-express-ws">Node not working with express-ws</a></p></li>
  </ul>
  
  <p>If anyone can let me know what's going on that would be great.</p>