catch nodejs ERR_CONNECTION_REFUSED error - javascript

So basically, I have the following situation going on: I have a nodejs server running on port 80 and an apache server on port 8080 (the apache server is used for executing php files to a mysql database).
Everything is working fine, unless the nodejs server is down. The client can't get the main index.html file (or any file for that matter) , and therefore can't execute code which includes catching an ERR_CONNECTION_REFUSED error, thrown by the client. When the server is down, I would like to reroute the client to a page with the port :8080 behind the url, where the users can see that the server is down.
My question is how do I do this?
Thank you very kindly,
Zeno
EDIT: apache error:
[Sat May 23 22:09:46.096365 2015] [ssl:warn] [pid 5020:tid 324] AH01909: www.example.com:443:0 server certificate does NOT include an ID which matches the server name
[Sat May 23 22:09:46.112376 2015] [proxy_html:notice] [pid 5020:tid 324] AH01425: I18n support in mod_proxy_html requires mod_xml2enc. Without it, non-ASCII characters in proxied pages are likely to display incorrectly.
[Sat May 23 22:09:46.189482 2015] [core:warn] [pid 5020:tid 324] AH00098: pid file D:/xampp/apache/logs/httpd.pid overwritten -- Unclean shutdown of previous Apache run?
[Sat May 23 22:09:46.195484 2015] [proxy_balancer:emerg] [pid 5020:tid 324] AH01177: Failed to lookup provider 'shm' for 'slotmem': is mod_slotmem_shm loaded??
[Sat May 23 22:09:46.195484 2015] [:emerg] [pid 5020:tid 324] AH00020: Configuration Failed, exiting

You should be able to specify a fallback resource in cases where one resource is not available:
FallbackResource /index.php
This can be placed in an .htaccess file or within your apache config.
https://www.adayinthelifeof.nl/2012/01/21/apaches-fallbackresource-your-new-htaccess-command/
To handle the node server being down, you would need to have setup a reverse proxy server using apache or nginx. Then you can specify a fallback resource in case the place your proxy points to is down.
This is how to setup a reverse proxy in apache with fallback via balancer.
https://serverfault.com/q/87507/193671

Related

Unable to get CORS working despite "access-control-allow-origin => *" shown to be working

I have uploaded a Reactjs build to a subdomain (a web hosting service), but most of the Axios.get API requests are causing CORS errors. It does not seem to be the usual problem of allow-origin because that tests to be working.
I created .htaccess and put it in the subdomain root folder with the following code
<IfModule mod_headers.c>
Header set Access-Control-Allow-Origin "*"
</IfModule>
which I confirmed working with the online test tool that gave this result:
"HTTP/1.0 200 OK =>
Connection => close
content-type => text/html
last-modified => Mon, 08 Aug 2022 03:52:36 GMT
accept-ranges => bytes
content-length => 644
date => Mon, 08 Aug 2022 02:39:20 GMT
server => LiteSpeed
access-control-allow-origin => *
alt-svc => h3=":443"; ma=2592000, h3-29=":443"; ma=2592000, h3-Q050=":443"; ma=2592000, h3-Q046=":443"; ma=2592000, h3-Q043=":443"; ma=2592000, quic=":443"; ma=2592000; v="43,46""
when I test my URL with test-cors.org it also shows to be working:
Sending GET request to https://subdomain.mysite.com
Fired XHR event: loadstart
Fired XHR event: readystatechange
Fired XHR event: readystatechange
Fired XHR event: progress
Fired XHR event: readystatechange
Fired XHR event: load
XHR status: 200
XHR status text: OK
XHR exposed response headers:
content-length: 287
content-type: text/html
last-modified: Mon, 08 Aug 2022 03:52:36 GMT
Fired XHR event: loadend
but my site produces this error in the browser inspector:
Cross-Origin Request Blocked: The Same Origin Policy disallows reading
the remote resource at
https://query2.finance.yahoo.com/v8/finance/chart/MYM=F?region=US&lang=en-US&includePrePost=&interval=15m&range=2d.
(Reason: CORS header ‘Access-Control-Allow-Origin’ missing). Status
code: 200.
I can only think that the server cache needs clearing, as I found a lot of people with problems similar suddenly find it working after a few days, but I need to know if it is something I am doing wrong rather than wait days for a cache refresh.
Thanks to #jaromanda-x in the comments I resolved this by approaching it differently.
I cloned a CORS ANYWHERE proxy server on Heroku and added the subsequent URL created onto the front of my axios.get requests for my React build. After it tested working, I whitelisted it with only my domain address so that the public facing CORS proxy server would not be abused.
The steps were as follows:
create a free account with Heroku and download their CLI and login from there. I then cloned CORS ANYWHERE to my local machine from that CLI
git clone https://github.com/Rob--W/cors-anywhere
cd cors-anywhere
then created an app on heroku with
heroku create
it makes it with a random name you can then change via the platform or in the CLI.
Once that was done I deployed it from the CLI with
git push heroku master
This created a public address I was able to put on the front of my axios.get URL requests in React so they now looked like this
axios.get('https://mycorsproxyapp.herokuapp.com/https://query2.finance.yahoo.com/v8/finance/chart/MYM=F?region=US&lang=en-US&includePrePost=&interval=15m&range=2d')
That tested working fine on my live React build site, so the final part was to lock it down to only my site, and to limit the risk of my own site abusing it accidentally, I limited the rate to 60 per minute. This was done from the local Heroku CLI or in the platform settings.
heroku config:set -a mycorsproxyapp CORSANYWHERE_WHITELIST=https://myreactsite.com
heroku config:set -a mycorsproxyapp CORSANYWHERE_RATELIMIT="60 1"

SSL Error - wrong version number (HTTPS to HTTP)

I receive an error when i try to "redirect" and https request to http by a stunnel server.
construction:
flask server: serve https which includes an button with a https request to another server (stunnel4).
stunnel: receives the request and redirect it to a local (http) server.
the stunnel server failes by following error:
Mär 27 19:27:20 raspberrypi stunnel[1400]: LOG5[1744]: Service [https] accepted connection from <external_IP>:51874
Mär 27 19:27:20 raspberrypi stunnel[1400]: LOG5[1744]: s_connect: connected <local_IP>:7777
Mär 27 19:27:20 raspberrypi stunnel[1400]: LOG5[1744]: Service [https] connected remote server from <local_IP>:45444
Mär 27 19:27:20 raspberrypi stunnel[1400]: LOG3[1744]: SSL_connect: 1408F10B: error:1408F10B:SSL routines:ssl3_get_record:wrong version number
is it possible that this error caused by my manually (not officially certificatied) created SSL certificates?
flask server --> uses an manual created cert and key file.
stunnel4 uses --> uses an manual created cert and key file (but different to flasks SSL files)
if you know any other methods to "redirect" the https to http, please comment.

Websocket over wss:// disconnects without any errors

I am making a webcoket application for my website. Evrything were OK before I tried secured connection.
Chrome just disconnects with "undefined" error.
Server console shows
2014-07-25 15:24:50 [info] [client 5.ХХХ.ХХХ.ХХХ:54166] Connected
2014-07-25 15:24:50 [info] [client 5.ХХХ.ХХХ.ХХХ:54166] Disconnected
My server is running CentOS 6 with Apache and PHP 5.4.
For WS server I choose Link1
Certificate also was built for TSL. What I am doing wrong?
You can check it out on Link2
Solved.
I replaced built-in SSL with stunnel.

Apache / XAMPP for Windows incorrectly treats file as executable

I'm running XAMPP 1.8.1 with Apache/2.4.3 (Win32) OpenSSL/1.0.1c PHP/5.4.7 on my dev machine to test my projects. In both my private project and in well known Bootstrap Datepicker component I can choose any language (one of 38 in case of datepicker) but not Polish.
Upon deeper investigation, I found out that this is caused, because when browser tries to load locale file (general.pl.json, in case of my project and bootstrap-datepicker.pl.js, in case of Bootstrap Datepicker), server (Apache) fails with 500 Internal Server Error.
After analyzing Apache error.log file, I found out, that this is happening, because Apache somehow is trying to execute this file as (probably Perl) executable script:
[win32:error] [pid 5128:tid 1680] [client 127.0.0.1:53455] AH02102: C:/XAMPP/htdocs/mobile/public/pg-demo-bootstrap/locales/general.pl.json is not executable; ensure interpreted scripts have "#!" or "'!" first line, referer: http://127.0.0.1/mobile/public/pg-demo-bootstrap/
[cgi:error] [pid 5128:tid 1680] (9)Bad file descriptor: [client 127.0.0.1:53455] AH01222: don't know how to spawn child process: C:/XAMPP/htdocs/mobile/public/pg-demo-bootstrap/locales/general.pl.json, referer: http://127.0.0.1/mobile/public/pg-demo-bootstrap/
[win32:error] [pid 5128:tid 1644] [client 127.0.0.1:53465] AH02102: C:/XAMPP/htdocs/us/ustv/assets/6dafd2fe/js/locales/bootstrap-datepicker.pl.js is not executable; ensure interpreted scripts have "#!" or "'!" first line, referer: http://127.0.0.1/us/ustv/content/manage/update.html?id=4
[cgi:error] [pid 5128:tid 1644] (9)Bad file descriptor: [client 127.0.0.1:53465] AH01222: don't know how to spawn child process: C:/XAMPP/htdocs/us/ustv/assets/6dafd2fe/js/locales/bootstrap-datepicker.pl.js, referer: http://127.0.0.1/us/ustv/content/manage/update.html?id=4
I did a lot of tests with changing contents and file names, using many fake files to pretend this file (Polish locale) and it all brought conlusion, that content is not a problem, only .pl in filename makes trouble.
Good questions are:
Why Apache claims, this is script, though .pl (Perl?) part of filename is in the middle and filename actually ends with .js or .json?
Why Apache for Windows is trying to execute Linux/Unix/Bash scripts and is looking for #! or '! characters in the first line of it?
Even better question is, how to fix this, so Apache would start treating this file as a simple Javascript, like all other locale files? And would not attempt to execute it?
In xampp\apache\conf\httpd.conf, there should be a line like:
AddHandler cgi-script .cgi .pl .asp
Simply comment out this line like so:
#AddHandler cgi-script .cgi .pl .asp
and restart Apache. If you want to keep the .cgi and .asp handlers, just delete .pl from the line. Perl will actually still work even if you do this.
I ran into the same issue, the previous answer did not solve the problem for me, but lead me to the Apache docs at http://httpd.apache.org/docs/2.2/mod/mod_mime.html#addhandler which says:
Filenames may have multiple extensions and the extension argument will
be compared against each of them.
The multiple extensions link lead to another section on the same page which explains that files can have multiple extensions. Ie.: welcome.fr.html is treated the same as welcome.html.fr. The order of extensions is not relevant and a filename can even contain multiple languages as in welcome.html.en.de. The advantage is that the defined languages are sent in the HTTP headers.
Configure handler based on final extension only:
<FilesMatch \.pl$>
SetHandler cgi-script pl
</FilesMatch>
To make this work I had to remove the handler for pl first:
RemoveHandler pl
If appears in apache log the following message
apache: .js is not executable; ensure interpreted scripts have "#!"
You should modify config file and use Alias instead of ScriptAlias like below :
Alias /bugzilla/ "C:/bugzilla/"
And add the below snippet in apache config after alias line.
<Directory "C:/bugzilla">
ScriptInterpreterSource Registry-Strict
AddHandler cgi-script .cgi
Options +ExecCGI +FollowSymLinks
DirectoryIndex index.cgi index.html
AllowOverride Limit FileInfo Indexes Options AuthConfig
Require all granted
</Directory>

Design.io can't hot-push code

Trying to use design.io with node.js Express to get css/javascript hot-push into browsers,
I cloned the example https://github.com/viatropos/design.io-example
and couldn't get it to hot deploy after
following instructions in https://github.com/viatropos/design.io-example/README.md
shell-1-project-dir> design.io --watch ./src
error: unknown option `--watch'
following the instruction on https://github.com/viatropos/design.io/README.md
shell-1-project-dir> design.io start
shell-2-project-dir> design.io watch
[Sun, 06 May 2012 03:52:04 GMT] INFO updated views/.index.jade.swp
[Sun, 06 May 2012 03:52:04 GMT] INFO updated views/index.jade
[Sun, 06 May 2012 04:03:11 GMT] INFO updated views/.index.jade.swp
[Sun, 06 May 2012 04:03:11 GMT] INFO updated views/index.jade
Doing this, I can't access the http://localhost:4181/ because node.js isn't started. I have to start node.js instead.
shell-1-project-dir> node server.js
However this doesn't hot-push the changed index.jade file.
Seems like the example is outdated?
How do I hot deploy ?
Env:
OSX-LION
node 0.6.15
First, you can have your node server running in a different terminal window (you don't need to run either the design.io server or the node server, you can run both at the same time as long as they are on different ports).
Second, I don't think this does what you want it to. Design.io seems to be for injecting changes made to static client files like stylesheets and javascript files. You change a .css file, design.io sees the change and broadcasts it to the browser, design.io in the browser forces a stylesheet reload, completing the hot push of the change.
Jade files are a different story, they need to be processed by an interpreter before being sent to the browser (browsers don't understand jade files). Design.io will see the change but can't do anything about it since Node.js needs to process the new file and send an updated response to the browser. The only way it does this is if you refresh the browser page (thereby sending a new request) which is not really a hot swap.

Categories

Resources