2

I have been stuck on this "socket hang up" error for a couple days now, and I was hoping someone could help me.

I currently have two Node programs set up:

  1. An HTTP server in Node that responds with the same data for every request.
  2. An HTTP server which responds with data from HTTP server 1. for every request.

My code for HTTP server 2. is below.

var http = require('http')
  , https = require('https')
  , server = http.createServer(do).listen(801)
function do(req, res) {
  var getReq = htts.get('http://127.0.0.1', function (getRes) {
    setTimeout(function () {getRes.pipe(res)},1000)
  })
  req.on('close', function () {
    // setTimeout(function () {getReq.abort()},20)
    console.log(++i + ': req closed')
    getReq.abort()
  })
}

The problem is when I send a request to HTTP server 2. and close the request before a response is sent to my browser (I've set a timeout to give me time to abort). If I continually hold down the refresh button, I will receive the "socket hang up" error after an x amount of times, and do not really understand how to fix this. If I set a timer before executing getReq.abort(), the problem happens less often, and if I set the timer to a large number (above 100ms), there isn't an issue at all.

I can consistently replicate the error by executing getReq.abort() right after creating the request, so I believe that it has something to do with aborting between the time the socket is assigned to the request and before the response is sent.

What is wrong with my code, and how do I prevent this from happening?

Thanks

Antoine
  • 607
  • 7
  • 10
  • you're creating a server off of a function that doesn't exist yet. That's pretty magical, how did you make that work at all? Also, and this is important: why do you need two servers running? E.g. what are you *actually* trying to achieve that you think requires two servers instead of one server that simply makes use of normal Node.js modules to do its thing? – Mike 'Pomax' Kamermans Nov 25 '14 at 00:02
  • Regarding the function that doesn't exist yet, I have no idea. I just tried it, and it worked. What I'm trying to actually achieve is maintaining a SSL connection with the browser but still be able to pull data, images specifically, from another website. The only way to do this is to make the Node program request the data, and not the browser. HTTP server 2 would make the HTTPS connection with the browser, and HTTP server 1 would be the third party website that I am not associated with. – Antoine Nov 25 '14 at 00:10
  • Then you don't need two servers. You just need your https server for clients to connect to, and the [`request` package](https://www.npmjs.org/package/request) to pull data/images/etc from other websites as and when they are needed (yes, that's over 5 million downloads in the last month. It's *the* most common packages used for retrieving website data) – Mike 'Pomax' Kamermans Nov 25 '14 at 00:11
  • your edit does not change the response. You only need one server. – Mike 'Pomax' Kamermans Nov 25 '14 at 00:12
  • If you read my edit, I mentioned that HTTP 1 emulates the website I am pulling data from. – Antoine Nov 25 '14 at 00:13
  • If you declare a function like `function name() {}`, it automatically gets 'pulled' to the top of the file/closure/scope. Function declarations like `var name = function() {}` do not. – nickclaw Nov 25 '14 at 00:13
  • I've played around with the request module you linked, Mike, and it looks like it also failed to pipe the response after an x amount of requests, although it is different than the error I am receiving. With the request module, no error is thrown. The response is just not sent to the browser. – Antoine Nov 25 '14 at 00:42

0 Answers0