Search code examples
node.jswebsocketnetwork-programmingnamecheap

Node.js Error: Unexpected server response: 301 - Running a Node.js Websocket server on Namecheap Webhost


I'm trying to setup a Node.js Websocket server over at my webhost (Namecheap). However, I am getting this error:

    events.js:292
      throw er; // Unhandled 'error' event
      ^

Error: Unexpected server response: 301
    at ClientRequest.<anonymous> (D:\****\node_modules\ws\lib\websocket.js:576:7)
    at ClientRequest.emit (events.js:315:20)
    at HTTPParser.parserOnIncomingClient [as onIncoming] (_http_client.js:596:27)
    at HTTPParser.parserOnHeadersComplete (_http_common.js:119:17)
    at TLSSocket.socketOnData (_http_client.js:469:22)
    at TLSSocket.emit (events.js:315:20)
    at addChunk (_stream_readable.js:295:12)
    at readableAddChunk (_stream_readable.js:271:9)
    at TLSSocket.Readable.push (_stream_readable.js:212:10)
    at TLSWrap.onStreamRead (internal/stream_base_commons.js:186:23)
Emitted 'error' event on WebSocket instance at:
    at abortHandshake (D:\****\node_modules\ws\lib\websocket.js:694:15)
    at ClientRequest.<anonymous> (D:\****\node_modules\ws\lib\websocket.js:576:7)
    [... lines matching original stack trace ...]
    at TLSSocket.Readable.push (_stream_readable.js:212:10)

The code for the client and server came from here:

https://github.com/websockets/ws#Simple server https://github.com/websockets/ws#sending-and-receiving-text-data

with ws://www.host.com/path changed to ws://<mydomainname>/<dir1>/<dir2>

Both client and server code runs properly when run locally. Http related code runs properly when used on my webhost. I am trying to get the client code to run locally and connect via websockets to my server hosted at Namecheap.

I suspect something related to the server is preventing connection. I'd ask support but I doubt if they can resolve this. Maybe there's a locked feature or something? Is there something else I could have missed?


Solution

  • Websockets cannot work on Namecheap's hosted servers. Their incoming ports are blocked and cannot be opened for security reasons per their customer's support.

    If anyone is looking for a solution just in case, try out a Heroku free account.