Fetch request to Express server stall using PERN stack - node.js

When I make a fetch request from my react front-end (localhost:3000) to my Express back-end, my browser console advises "ERR_EMPTY_RESPONSE" and the network tab of my developer tools advises the connection stalled.
I've done some troubleshooting with my back-end and the problem happens intermittently, only when I try to make a call to my database (postgreSQL). There are no errors logged or caught. the res.end() does not fire.

I figured this out after a day of trial and error and research. My nodemon was restarting my server between calls because some of my middleware was saving new files to my server. This meant the server restarted between sending/receiving calls to my database.
I resolved the issue by adding nodemonConfig to my package.json in order to ignore files and directories when triggering refreshes.

Related

expressjs server hangs at random

I am running an express application which uses imagemagick to take a screenshot of a webpage and upload the result to S3 and insert a row into our database.
The service works well however after a few requests eventually the server just hangs upon request. There is no response, but the server is technically not 'down'.
Is there any way for me to see logs, or anything that can be causing this such as memory leak?
You should profile your application to check when the bottleneck happens and where it happens in your code. A good starting point is with node's integrated V8 stack profiling module.

How to detect and possibly ignore processing a bad/hung client browser request

I'm developing a node web application. And, while testing around, one of the client chrome browser went into hung state. The browser entered into an infinite loop where it was continuously downloading all the JavaScript files referenced by the html page. I rebooted the webserver (node.js), but once the webserver came back online, it continued receiving tons of request per second from the same browser in question.
Obviously, I went ahead and terminated the client browser so that the issue went away.
But, I'm concerned, once my web application go live/public, how to handle such problem-client-connections from the server side. Since I will have no access to the clients.
Is there anything (an npm module/code?), that can make best guess to handle/detect such bad client connections from within my webserver code. And once detected, ignore any future requests from that particular client instance. I understand handling within the Node server might not be the best approach. But, at least I can save my cpu/network by not rendering to the bad requests.
P.S.
Btw, I'm planning to deploy my node web application onto Heroku with a small budget. So, if you know of any firewall/configuration that could handle the above scenario please do recommend.
I think it's important to know that this is a pretty rare case. If your application has a very large user base or there is some other reason you are concerned with DOS/DDOS related attacks, it looks like Heroku provides some DDOS security for you. If you have your own server, I would suggest looking into Nginx or HAProxy as load balancers for your app combined with fail2ban. See this tutorial.

Trying to login into application using NodeJS and Sails.JS and receiving server timeout on XHR Polling

I am working on a NodeJS application that uses Sails.JS, that requires a login to get to the dashboard of the application.
Originally I was receiving errors with websockets and used this solution to get past this issue.
Receiving error "connection to ws://localhost:1337/socket.io/1/websocket" was interrupted while the page was loading"
Now, I have another problem, where the application is xhr-polling and not connecting to anything, and the server timesout and never redirects to the dashboard. This behavior is not happening on some peeers computers, they are able to login successfully. I am trying to figure out why the xhr polling runs and does not redirect me to the dashboard after successful login. Why is this only happening on my machine and not theirs. Any advice would be greatly appreciated.Simply boggles my mind.
Thank You

How do I find out where my Express app the response is being sent/finished?

I'm getting Error: Can't set headers after they are sent in my app. I know a response is being sent before the response I intend to deliver, but I'm not sure where that's coming from. Can I log to the console when a respnose has been sent? I'm currently using morgan for logging, but it only seems to log requests. I can't find a way to log every response as well.
If not, what is another effective way to hunt this problem down?
Update: This app runs on Azure and uses the Azure SDK for node or else I would try running it through the node debugger or node-inspector. I don't think there's a way to do this while also running the app in the emulator. I could be wrong about that.
Your error says that there was a response send already, check your code with the node-inspector or any other debugging tool and search for the spot.
Here the node-inspector.
And since you work with Azure this should help you getting started with the node-inspector.

Requests to api on Heroku-hosted NodeJS app not making it to app

I'm hosting a NodeJS+Express+Mongoose app on Heroku and have been struggling to understand why some of my GET requests are serving stale data, after repeated changes to resources using the api. The changes only get reflected through the GET call after about half an hour or if I restart the server.
The stale resource data gets returned for requests from multiple clients, my Angular JS webapp as well as curl. So, I'm guessing it isn't something to do with caching on the client.
Based on the logging it looks like the requests aren't even making it to the Express app routes, so I'm guessing there's some kind of caching that's happening on the server. I don't see this behaviour on my local system, so is it something that can be disabled on Heroku?
Generally, if you are using Express then your request passes through multiple Express Middleware before actually reaching your app routes. You have to see if there is some error in any of those middleware or there is some validation error for the data which you feed in.
I would suggest you to exhaustively test the data which failed on the heroku app on your local app too.

Resources