2

Its almost a week I'm having trouble with putting my node.js application online. I have a shared hosting where I created an subdomain (let's say sub.domain.com) and this subdomain is pointed to an directoy /home/wproj/myapp which contains code of my nodejs application. I logged into server using SSH and executed following commands exactly.

cd myapp
node index.js

Now node application is started on port 3000 but I cannot access its webpage from browser. so I used an .htaccess file which contains following code.

RewriteEngine On
RewriteRule ^$ http://127.0.0.1:3000/ [P,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ http://127.0.0.1:3000/$1 [P,L]

Now i can see the webpage at http://sub.domain.com/. But problem is my server is emitting an socket but client is not able to connect to that socket. On local server it was working perfectly fine, I don't seem to find any solution from hundreds of already asked questions I visited. In browser console, it says that

Firefox can’t establish a connection to the server at ws://sub.domain.com/socket.io/?EIO=3&transport=websocket&sid=pItfylCcSBwvBrGaAAAZ.

My client side code looks like this.

const socket = io.connect("http://sub.domain.com/");

I also tried entering port number like this

const socket = io.connect("http://sub.domain.com:3000/");

it didn't worked either. Also tried to replace 127.0.0.1:3000 with http://sub.domain.com:3000 and still socket is not connecting and always returns 404

I'm sorry for the long question, but I had to tell the whole story. If anyone can help me or point me in the right direction, I'll be grateful.

Thanks.

Asad ullah
  • 620
  • 2
  • 9
  • 26
  • Since you're mentionning a `.htaccess` file, I assume you're using Apache. You'll need to enable `proxy_wstunnel` on it, and configure a reverse proxy to bind incoming ws connections to port 3000. More info [here](https://stackoverflow.com/a/27534443/1913729) – blex Feb 21 '20 at 00:01

1 Answers1

1

You need to have the port 3000 open for inbound and outbound traffic. Since it is a shared hosting you need to check if you can do this by yourself from the admin panel (cPanel for example) before writing a ticket to the support.

You can run this command grep -w 3000/tcp /etc/services to see if the port appears in the running services list.

You can investigate the network panel from Web Development Tools (Ctrl + Shift + I in your browser). If you see the No 'Access-Control-Allow-Origin' header is present on the requested resource. error then you need to start your server like this: io = require('socket.io')(httpServer, {'origins': 'sub.domain.com:*'})

ajaysinghdav10d
  • 1,771
  • 3
  • 23
  • 33
bnitica
  • 11
  • 4
  • if port 3000 is not opened then node shouldn't start my application I guess. If application is starting and I can see some of its content on a webpage, that means port 3000 is open alreay – Asad ullah Feb 21 '20 at 00:13
  • @Asadullah The port being closed does not mean you cannot start your application. Your server just won't let it communicate with the outside world. That being said, yes, if you can see parts of your application running on that port, then it is open. But apparently only on the `http(s?)` protocol – blex Feb 21 '20 at 00:16
  • You said that you see http://sub.domain.com/ this means port 80. The app may start on any port but for access it over the web the port 3000 should be open. – bnitica Feb 21 '20 at 00:19
  • @bnitica yes I can see it on sub.domain.com and that's what confusing me more, because I'm accessing it on port 80 and it is showing me some part of app. just sockets are not connecting. however when i try to access from this URL sub.domain.com:3000 it loads for 15 - 20 seconds and then gives error. ok i'll make sure if port 3000 is open or not, any way to check it from SSH? – Asad ullah Feb 21 '20 at 00:23