how to connect to socket.io on live server using nodejs - javascript

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.

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:*'})

Related

How to do a reverse proxy with node.js for a shiny app

EDIT
I think the problem might be that the WebSocket connection does not go through the proxy node.js. How to authorize the connection of the WebSocket?
I have a web app hosted on a nodeJS server. On the same vm I also have a shiny serveur hosting an app. I use node to redirect traffic to port 3838 (shiny) when a somes URL are requested.
I use this code on node :
app.use('/the-shiny-app', proxy({target: 'http://localhost:3838', changeOrigin: true}));
With this setting everything works fine on the shiny app when I go on mydomain/the-shiny-app/* except when I try to run code in a code box.
When I try to run code I get this error on the chrome console :
Connection closed. Info: {"type":"close","code":4503,"reason":"The application unexpectedly exited","wasClean":true}
An example of what I mean by code box :
if I do not use node.js and I redirect the traffic (on OS level) from port 80 directly to 3838 everything works fine.
sudo iptables -A PREROUTING -t nat -p tcp --dport 80 -j REDIRECT --to-ports 3838
I do not know exactly what kind of exchange is made between the browser and the shiny server when we run code, but maybe the protocol used is blocked by node.js.
I found the problem. As Antony Gibbs said you need to setup a WebSocket upgrade. I'm using http-proxy-middleware you cans find the doc here : https://github.com/chimurai/http-proxy-middleware

URLs don't work within production - return 404

This issue seems to be well documented but all solutions people suggest on posts don't work for me :/
I managed to fix urls giving 404 on my localhost by applying
--history-api-fallback
to the "scripts" package.json
however when I move the production over to my apache2 setup, any url I manually type or refresh returns 404, on site navigation works fine.
for example, http://www.craftmc.net/ works but http://www.craftmc.net/login returns 404, despite that url working if i navigate to it on site.
I saw a post saying use this .htaccess,
https://hastebin.com/alifavacil.apache
Sadly it didn't work. :/
navigation in question - https://hastebin.com/xevecefoda.xml
You need to configure Apache to serve your app on all URLs that it's supposed to handle.
By default, Apache will only serve your app when the user requests the specific path where the app is located, say for example www.example.com
If you now try to visit www.example.com/user/123, Apache will try to find a file named "123.html" inside the "user" folder. You have to tell Apache that what it should do is reply with your app even if the path requested by the browser doesn't actually point to your app within the filesystem on the server.
You can do this in several ways, but a common one is to use mod_rewrite. For example, you might try something along these lines:
DocumentRoot "/var/www/example.com"
AliasMatch "^/myapp" "/opt/myapp-1.2.3"
<Directory "/opt/myapp-1.2.3">
RewriteEngine On
RewriteBase "/myapp/"
RewriteRule "^index\.html$" "welcome.html"
</Directory>
For more information about mod_rewrite and the various directives it uses, refer to the documentation, and/or google.

ERR_CONTENT_DECODING_FAILED for one file after Joomla update

After updating Joomla to 3.8.1 today most of the admin tabbing and navigation functions stopped working in Chrome, FireFox, Win Safari, and IE 11. media/jui/js/bootstrap.min.js was failing to load with ERR_CONTENT_DECODING_FAILED. It still works in MS Edge.
This is occurring on machines in multiple locations and over multiple ISPs.
Actions taken (none of which have resolved):
Deleted file from server and replaced with backed up copy from last week and re-issued chmod 0644
Verified that public $gzip = '0'; in configuration.php
Tested in MS Edge where the error did not occur
Used MS Edge to go to Joomla Global Configuration > Server Settings and set Gzip Page Compression to No. Had been Yes.
Found that in browsers where it was failing if JS file is accessed without the versioning query string the browser can load file without error.
Found in Safari, IE 11, and iPhone Chrome the JS file displays and appears to be gzipped or otherwise encoded. Screenshot:
Cleared browser cache in Chrome
Went to chrome://net-internals and clicked Flush Socket Pools and Close Idle Sockets
Cache settings checked and it has been set to off the whole time:
System - Page Cache module also disabled
In this case the client had failed to mention they had added Sucuri Firewall service and that was where they corrupted cached file was being delivered from. Cleared cache at Sucuri and file no longer throws ERR_CONTENT_DECODING_FAILED
I had the same situation. Gzip on 0 in configuration.php did not work. So I found out that get_magic_quotes_gpc was deprecated in
libraries\src\Application\AdministratorApplication.php.
In my case I solved it by editing the default htaccess that my Joomla 4.2.X came with.
You have to leave these lines activated.
Header always set Cross-Origin-Resource-Policy "same-origin".
Header always set Cross-Origin-Embedder-Policy "require-corp".
And you have to remove this code:
<IfModule mod_headers.c>
# Serve gzip compressed CSS files if they exist
# and the client accepts gzip.
RewriteCond "%{HTTP:Accept-encoding}" "gzip"
RewriteCond "%{REQUEST_FILENAME}\.gz" -s
RewriteRule "^(.*)\.css" "$1\.css\.gz" [QSA]
# Serve gzip compressed JS files if they exist
# and the client accepts gzip.
RewriteCond "%{HTTP:Accept-encoding}" "gzip"
RewriteCond "%{REQUEST_FILENAME}\.gz" -s
RewriteRule "^(.*)\.js" "$1\.js\.gz" [QSA]
# Serve correct content types, and prevent mod_deflate double gzip.
RewriteRule "\.css\.gz$" "-" [T=text/css,E=no-gzip:1]
RewriteRule "\.js\.gz$" "-" [T=text/javascript,E=no-gzip:1]
<FilesMatch "(\.js\.gz|\.css\.gz)$">
# Serve correct encoding type.
Header append Content-Encoding gzip
# Force proxies to cache gzipped &
# non-gzipped css/js files separately.
Header append Vary Accept-Encoding
</FilesMatch>

PHP Web Server - Change localhost to custom domain name?

I am currently creating a Laravel project and need to store cookies in a http://localhost:8000 address, of course, I have found out that to set a cookie through JavaScript, the domain must have two or more periods (.) in.
Obviously, with the address set to localhost:8000, cookies fail to be stored. I need to be able to fake my host to point something like http://dev.project.laravel as opposed to localhost:8000
Apologies if I'm not making sense, but hopefully you catch my drift. Thanks in advance for any help given.
Try the following:
Assuming you have Windows OS and have installed XAMPP (or similar) stack:
Add the following line to httpd.conf file under # Supplemental configuration which is in the configuration folder of apache... (I have XAMP installed and for me, this path is: C:\xampp\apache\conf)
Include conf/extra/httpd-vhosts.conf
it should be around line 484
if you already have that line, but its commented out, then un-commment it.
Add the following to your httpd-vhosts.conf file (which, in my case, is located at C:\xampp\apache\conf\extra)--
In the first line below, change 80 to whatever port your localhost is running on... if you do not use port to access localhost, then leave it as 80)
<VirtualHost *:80>
DocumentRoot "C:/xampp/htdocs/"
ServerName desired.name.of.your.site
ServerAlias desired.name.of.your.site
</VirtualHost>
And lastly, and most importantly, add the following in your hosts file (for me, this is located at C:\Windows\System32\drivers\etc) and then restart apache
127.0.0.1 desired.name.of.your.site
If you have a LAMP stack, the above should still apply... just the folder paths would need to change.
Hope this helps!
Best,
-Rush

Node.js script loading not loading from browser (outsider) but loading from command (localhost)

I have successfully installed Node.JS and Balloons.IO chatroom on my linux based vps (with SSH). When typing curl http://mydomain.com:9191/ in the ssh command I can see the html is loaded. But, when trying to reach the same page from my browser it takes some time loading then says page could not be reached. Any idea why ?
My common diagnostic steps:
1) Check that your app is actually listening on the port it should be, you can do this with:
sudo netstat -anp | grep :9191
You should see your app listening to 0.0.0.0:9191 or your.ip.address.here:9191 if you see something like 127.0.0.1:9191, then it is only listening locally so you won't be able to reach it.
2) Ensure your firewall isn't blocking these ports, if you are using iptables you can check with:
sudo iptables --list
This will print the rules for your firewall and you can check if you port is blocked (or allowed).
3) Try connecting locally. My third step is generally to try it locally with curl, you did this step already but for other landing here you can do something like:
curl http://localhost:9191/
and see what you get back
4) Try connecting remotely. If everything above looks fine, try running a verbose curl from a remote host and see what you get:
curl -v http://mydomain.com:9191/
This will show header and body output so you can see if the remote host even responds; if it doesn't then check if the raw port is even accessable with telnet:
telnet mydomain.com 9191
which if successful will print something like:
Trying your.ip.address.here...
Connected to mydomain.com.
Escape character is '^]'.
If it fails it will just hang at Trying... if it fails then your firewall is blocking the port, your host is blocking the port, or your app isn't listening to the port. If your above tests passed then contact your host because something else may be up and you should be able to get support from them.

Categories