Node.js Server Response Error sometime (on High load) - node.js

The nodejs server response error message on high load.
Some of the errors are:
Http failure response for (api url):0 Unknown Error and cors error. On the browser console.
On Android side the error is unexpected end of stream on Connection {api url:80, proxy=Direct hostaddress=apiurl/serverip:80 cipherSuite=none protocol=http/1.1
We have 16 GB ram 4vCPUs aws instance
nginx rever proxy to loadbalance between 4 instance of node (created through pm2), ip_hash (as we are using socketio)
while looking through the web some suggestions were "Its cors error", "it server side error, contact the backend team"
for cors We am using cors package from npm. app.use(cors()). I am looking for the server side fixation. we are using express.
since the website and app works perfectly while its working, and we get cors error during the problem. it may not be the cors error. during the high load, we get the error instantly, not time out error.
We could not figure out the issue. any suggestions?
Update: Since there are voting going on to close this, I may not get the answer, but here are some errors, Please note that the app and website (angular) works fine but got these error during pick hours, I expect timeout or node to stop, but not these errors so, thought something is wrong.
Update2: While looking though the nginx error log, we get this error.
2021/03/09 17:10:25 [alert] 1058#1058: *2946806 768 worker_connections are not enough while connecting to upstream, client: 120.xx.xxx.114, server: api.mydomain.com, request: "GET /Image-1611905660330.jpg HTTP/1.1", upstream: "http://127.0.0.1:8000/Image-1611905660330.jpg", host: "api.mydomain.com"
Update 3: Looking for the above error, found suggestion that, we need to increase worker_connections, so we update it on /etc/nginx/nginx.conf;. Now we get error *2908 socket() failed (24: Too many open files) while connecting to upstream,
Update 4: Adding worker_rlimit_nofile 20000; to the /etc/nginx/nginx.conf seems to fix the error on update 3.

The cors error is emit by the browser, not the server. server just gives browser cors policy response. Whether it should be treated as error or not is decided by the browser itself.
I'm not android developer, but i think some of your problem is made by misused request from android client, not server.
Android connect Node.js REST unexpeceed end of stream
java.io.IOException: unexpected end of stream on Connection in android

Related

Why is my HttpPlatformHandler server app getting HTTP Error 502.3 Bad Gateway

I'm writing a HttpPlatformHandler server app with C++ and Winsock functions. It works great on my development machine but when deployed on other machines it fails. I can run it separately and access it from the browser using the default port I coded into it on those other machines. Here are the error details:
Web Browser:
HTTP Error 502.3 Bad Gateway
Error Code 0x80070057
There was a connection error while trying to route the request.
Event Viewer:
HttpPlatformHandler
Process '0' failed to start. Port = 19516, Error Code = '-2147024809'.
I figured it out. I had the web.config file in a subfolder under C:\inetpub\wwwroot with the file in the URL. I moved it to C:\inetpub\wwwroot and now it works.

AWS ElasticBeanstalk NodeJS - 502 error: recv() failed (104: Connection reset by peer) while reading response header from upstream

We are planning to migrate our NodeJS platform from plain EC2 to ElasticBeanstalk. During these process, after some struggles, we have deployed our app and able to access and perform actions. However, for some requests, we received 502 error.
After checking the logs we found below;
/var/log/nginx/error.log
2020/03/16 06:12:09 [error] 3009#0: *119488 recv() failed (104: Connection reset by peer) while reading response header from upstream, client: xxx.xx.xx.xxx, server: , request: "POST /www_auth/register HTTP/1.1", upstream: "http://127.0.0.1:8081/register", host: "****.us-east-2.elasticbeanstalk.com"
It occurs in randomly and I don't have any clue. I feel some configuration level changes I missed/need to add with nginx.
If you have any steps/suggestions to solve this, appreciate it!
AWS Elastic Load Balancer pre-connects to backend servers, and it can cause a race condition where ELB thinks a connection is open, but the Node.js backend has already closed it, due to the server.keepAliveTimeout of 5 seconds of idle inactivity, the default value in Node.js 8.x and newer.
Disable server.keepAliveTimeout and server.headersTimeout to work around this issue, or set these timeouts to a ms value larger than the AWS ELB's Idle Timeout value.
const app = express();
// Set up the app...
const server = app.listen(8080);
// Disable both timeouts
server.keepAliveTimeout = 0;
server.headersTimeout = 0;
Credit for this solution goes to Shuhei Kagawa:
https://shuheikagawa.com/blog/2019/04/25/keep-alive-timeout/

Nodejs crashes when reading from disk

I'm working on a website based on expressjs which streams videos and users can download video files from it.
when I start the app, everything works well. But after some while app can not stream videos and downloading files doesn't work too(it returns an empty file after a long delay) the only way to fix it is to restart the app.
nginx error log:
2017/01/16 19:39:39 [error] 10584#10584: *23717 upstream prematurely closed connection while reading upstream, client: x.x.x.x, server: example.com, request: "GET /api/course/.../video/.../stream HTTP/1.1", upstream: "http://127.0.0.1:8080/api/course/.../video/.../stream", host: "example.com"
I use pm2 in cluster mode .
I have this in the app for logging errors:
process.on('uncaughtException', function(err) {
console.log(err);
winston.error('Caught exception : ' ,err);
});
But there is no error in log files.
for streaming video I use an approach similar to this .
for downloading files I use express download function.
First, check winston daily rotate file, they have a transport for logging unhandled exceptions. Second, if after sometime your app becomes slow, it is possible that you have a memory leak because the garbage collection slows the process or the thread gets blocked by a long running synchronus code, it will be helpful if you show your code here.

Sails.js Socket Issue on Google App Engine

I'm running Sails.js (v0.11.0) on a Managed VM in Google App Engine, everything works fine provided I disable sockets. I am using the default Sails project, with one model (User) with no custom attributes and am simply visiting the home page in my browser. I see the following in Chrome's console window;
WebSocket connection to 'ws://localhost:8080/socket.io/?__sails_io_sdk_version=0.11.0&__sails_io_sdk_platform=browser&__sails_io_sdk_language=javascript&EIO=3&transport=websocket&sid=GShPZd_tjzqnrigNAAAA' failed: Error during WebSocket handshake: 'Upgrade' header is missing
sails.io.js:143
|> Now connected to Sails.
\___/ For help, see: http://bit.ly/1DmTvgK
(using browser SDK #v0.11.0)
4(index):1 Font from origin 'http://sailsjs.org' has been blocked from loading by Cross-Origin Resource Sharing policy: The 'Access-Control-Allow-Origin' header contains the invalid value ''. Origin 'http://localhost:8080' is therefore not allowed access.
sails.io.js:4 POST http://localhost:8080/socket.io/?__sails_io_sdk_version=0.11.0&__sails_io_s…ascript&EIO=3&transport=polling&t=1427404282042-3&sid=GShPZd_tjzqnrigNAAAA 503 (Service Unavailable)
sails.io.js:143 Failed to connect socket (probably due to failed authorization on server) Error: Error: xhr post error {type: "TransportError", description: 503, stack: (...), message: "xhr post error"}
sails.io.js:143 ====================================
sails.io.js:143 Socket was disconnected from Sails.
sails.io.js:143 Usually, this is due to one of the following reasons:
-> the server was taken down
-> your browser lost internet connectivity
sails.io.js:143 ====================================
sails.io.js:143
Socket is trying to reconnect to Sails...
_-|>_- (attempt #1)
sails.io.js:4 POST http://localhost:8080/socket.io/?__sails_io_sdk_version=0.11.0&__sails_io_s…ascript&EIO=3&transport=polling&t=1427404302073-4&sid=GShPZd_tjzqnrigNAAAA 503 (Service Unavailable)
sails.io.js:4 GET http://localhost:8080/socket.io/?__sails_io_sdk_version=0.11.0&__sails_io_s…sails_io_sdk_language=javascript&EIO=3&transport=polling&t=1427404303080-5 503 (Service Unavailable)
'Upgrade header is missing' seems like it may be the culprit, but I don't understand how the headers could have been changed from whatever Socket.io requires, I have an app.yaml file that simply allows passes all traffic to sails.
#app.yaml
module: default
version: 1
runtime: custom
api_version: 1
vm: true
manual_scaling:
instances: 1
handlers:
- url: /.*
script: app.js
Any help greatly appreciated!
Cheers! R.
I don't think you're going to be able to use Sails' web sockets out of the box. Google App Engine is not something I'm familiar with, but it appears you need to use their API for web sockets. See this example:
https://github.com/thebergamo/appengine-websocket-nodejs
After doing some more poking around I eventually came across this post, which is pretty much a duplicate of this one and should give you some more clues. The short answer is you need to have a separate Google Managed VM running your web socket service and then you access it directly via ip address, which seems to take all the fun out of using Sails.
Run Websocket on GAE

Websocket server always returns close state,but works fine in localhost

I get the source code of a great webrtc demo from
https://github.com/webRTC/webrtc.io-demo
and the demo site is:
webrtc.dennis.is
It works fine when I start a local node server, but when I upload the demo to a node server online( www.appfog.com ), I come into an unexpected bug which says :
WebSocket connection to 'ws://talktosomeone4.ap01.aws.af.cm/' failed: Unexpected response code: 200
talktosomeone4.ap01.aws.af.cm:1
At first I think maybe it's caused by missing port,so I try to complete the websocket url by adding port like :
ws://talktosomeone4.ap01.aws.af.cm:8080/
or the port I get from serve: process.env.PORT, the error message is gone but the websocket.onopen is never fired , it always fires the onclose event. In result, the webrtc doesn't work.
What's the problem? What should I do?
I'll appreciate it for your kind help!!!
AppFog does not yet support websockets. But they promise to add support soon.

Resources