Node Express Can't set headers after they are sent In case of Multiple Request - node.js

My Application works perfectly fine untill I send multiple (2 or more) request simultaneously. As soon as I send multiple requests Express throws
Error: Can't set headers after they are sent.
Its a long code, where its takes almost 2 seconds to execute complete request . If I send multiple requests (where response time is pretty fast) i dont face this issue?
I know the reason why and when express throws 'Can't set headers' error. My main point is to understand how I can debug my application in this scenario as everything works fine incase of single request and it breaks down incase of multiple requests.

The error means that you're already in the Body but later in the code some function tried to set a header or statusCode. When you see this error, try to look the code for any improper callbacks or exception scenarios in which the response object is referenced multiple times.
Usually it must be next() called twice .

Related

axios network error with cors activated still returns error

the link to the code link
I am using axios and nodejs.
All routes work and give a response except one. which returns Network error.
that route is /api/ads/myads.
The route works on its own but when used with redux actions it doesn't
The network tab says that this request was blocked.
So I tried to add cors but that didn't solve the issue.
The file in question is .../actions/adActions this one is producing the error while others don't
I will be uploading the code in a minute
This turned out to be my adblocker thinking this was actual ads

204 error code then 500 error code responses

So I have an application which needs to send data to the API which is created by our team leader using NodeJS with Express.js.
On my end I have laravel application which using VueJS for the UI. Inside the Vue JS component. I am using axios to request to the API.
axios.post('https://clearkey-api.mybluemix.net/sendcampaign', request)
.then(function(response) {
//console.log(response);
})
However, it returns 204 which means according to this https://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html.
204 No Content
The server has fulfilled the request but does not need to return an
entity-body, and might want to return updated metainformation. The
response MAY include new or updated metainformation in the form of
entity-headers, which if present SHOULD be associated with the
requested variant.
If the client is a user agent, it SHOULD NOT change its document view
from that which caused the request to be sent. This response is
primarily intended to allow input for actions to take place without
causing a change to the user agent's active document view, although
any new or updated metainformation SHOULD be applied to the document
currently in the user agent's active view.
The 204 response MUST NOT include a message-body, and thus is always
terminated by the first empty line after the header fields.
Then next it returns 500 Internal Server Error. So in my opinion it returns this error because there is no content to be returned from the server?
Can you tell me other possible problems why it return that response?
Check if the "HTTP method" of the 204 is OPTIONS and if the method of the 500 is POST.
If both are like that, then you are seeing first a CORS pre-flight request (the OPTIONS that returns 204) and then the actual request (the POST that returns 500).
The CORS pre-flight request is a special HTTP message your browser sends to the server when the webpage and the backend are hosted at different addresses. For example, if your website is hosted at http://localhost but the backend you are trying to access is hosted at https://clearkey-api.mybluemix.net.
The reason of the 204 just means your backend endpoint is correctly setup to handle requests for /sendcampaign (you can ignore it). The reason of the 500 is because of some exception in the implementation of the function that handles that endpoint.

Is it required to send a response in every request no matter GET or POST in nodejs?

I defined my_function inside app.post('/someRoute',my_function) in nodejs which is used for making an http-request(posting data) to another server.
However it seems that my_function will run twice when I do not defined any response to the browser inside my_function.
What will be the reason for this? And is it required to send a response in every request no matter GET or POST in nodejs?
Thanks!
Whenever you do not define a response to a function/route handling get or post requests, the request response cycle will not end and thus the request keeps running until it times out because it took too long to get a response from the server. It is thus important to define a response to every request.

Setting Error Pages for a specific path

I am using ECS for a webapp, and i setup an Error Pages definitions for codes 404 and 502. My current setup returns a static maintenance.html file (hosted on S3) and returns a code of 200. I have to return 200, otherwise Outlook (which hosts my app) will not render the returned html, and show an ugly error page instead.
The problem I'm having with it, is that a later API call from my clientside app to my server, might also return 404. With the current setup, CloudFront intercepts this reply, and returns the maintenance.html with code 200. So now my app doesn't know anything is wrong, and later fails on parsing the reply.
Is there a way to define the Error Pages to only handle specific requests? For my usage, I'd like them to only handle calls to my /static/index.html files. If some other file/API call is actually missing from the server, I'd like my client to get the 404, so it can handle it properly.
So to answer your question according to the documentation I read there is currently no way to do this from cloudfront Error Pages Config.
However, We had a similar problem, but in our case API calls were returning a 500 Error with a s3 custom error html instead of the actual server error that I expected only for API calls.
What we ended up doing in this case was a handler in the back end for 500 errors that would return a response code that we were not using in the cloudfront Error Pages if the request Url had the pattern of API calls (we used http Error Code 406) and then the handler will reply with the 406 response code and the whole error instead of the 500 with the s3 response html only when failed over the api url pattern. I know this does not solve the problem the way you would have like to, but probably you may find it helpful until cloudfront allows a custom error response based in a Path Pattern.

node.js parallel requests are crashing node app - can't render headers

Ways to replicate the problem,
A. continuously refresh the browser (cmd+r) and then the app crashes with the following message
B. run mocha test suite, inside I'm using npm module request for http request and all of them pass, however as soon as I send another request in parallel using postman while the tests are running, then the app crashes with the following message.
http.js:731
throw new Error('Can\'t render headers after they are sent to the client.'
^
Error: Can't render headers after they are sent to the client.
at ServerResponse.OutgoingMessage._renderHeaders (http.js:731:11)
at ServerResponse.writeHead (http.js:1152:20)
at puremvc.define.signInWithCredentialsSuccess (eval at include (/Library/WebServer/Documents/FICO/fico-node/app.js:3:32), <anonymous>:31:18)
the code referenced
signInWithCredentialsSuccess: function(request, response, authToken) {
response.writeHead(200, {'Content-Type': 'text/xml'})
response.end(this.mustache.render(this.successTemplate, {authToken: authToken}));
},
It's an express application combined with puremvc, so architecture is a bit different, for instance after app.get, the request and response are passed around to different decoupled modules and functions who accomplish their tasks and simply end the response by writing some output and then the process is repeated again for each subsequent request using new response/request objects passed.
I assume that I should be getting a new request and response object for each request but the error message confuses that for some reason the app.get is giving me a reference to an old response object which was already used to sent the information down to the client that's why it's refusing to modify the headers.
my node version is 0.10.32
please advise

Resources