-
Notifications
You must be signed in to change notification settings - Fork 15.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
http module error out while sending empty response body #2775
Comments
I raised the same issue in node community please checkout the detail here : nodejs/node#3269 (comment) |
Hi! This is indeed a Node.js core issue. You can find the following server will have the same issue and does not use Express at all: var http = require('http');
var server = http.createServer(function (req, res) {
switch (req.url) {
case '/test_1':
var options = {
'url' : 'http://localhost:3000/test_2',
'timeout': 10
};
require('request').get(options,function(err,response,data){
response = response || {};
data = data || {};
if (err) {
console.log("some error " + err);
}
res.statusCode = response.statusCode;
res.end(data);
});
break;
case '/test_2':
setTimeout(function(){res.end('ok');},100);
break;
}
});
server.listen(3000, function () {
console.log('Test app listening on 3000');
}); You're basically encountering two problems:
I hope this helps! |
And as an additional followup, here is the correct way to write your example in Express: var express = require('express');
var app = express();
app.get('/test_1', function (req, res, next) {
var options = {
'url' : 'http://localhost:3000/test_2',
'timeout': 10
};
require('request').get(options,function(err,response,data){
if (err) return next(err); // this is the key
res.status(response.statusCode).send(data);
});
});
app.get('/test_2', function(req,res){
setTimeout(function(){res.status(200).send('ok')},100);
});
var server = app.listen(3000, function () {
console.log('Test app listening on 3000');
}); |
Thank you dougwilson for your express reply. As you pointed out we should not proceed in the callback if there is a error. But in our production env, we are using node as a proxy server between app and backend engine. So in case of error we have to send some error response to app to let app know a problem occurred in server but for some api we do not have a default error response so we tried to send empty response and body to app but unfortunately node started crashing. Thanks again for the help. Have a good day :) |
Gotcha. By default |
Yes, next(error) is working as expected. I actually did not tried that example, sorry for that. |
Please consider below test case:
file test.js
If I will run this program and hit http://localhost:3000/test_1 I am getting below error:
Possible reason:
request time out is 10ms where as /test_2 return the response after 100ms so request module stop waiting for http response and return to call back with err = " ETIMEDOUT", response and data = undefined.
To stop breaking the code I have added
But code is breaking while sending the body.
Work around:
Note change :
response = response || {"statusCode" : 500};
With this change code is running fine.
Please let me know if I am doing anything wrong here or if there is any issue in http module.
Thanks
The text was updated successfully, but these errors were encountered: