“Socket hang up” error during request

后端 未结 7 771
北海茫月
北海茫月 2020-12-16 11:23

I try to make a GET request to some site (not mine own site) via http module of node.js version 0.8.14. Here is my code (CoffeeScript):

options = 
        ho         


        
7条回答
  •  失恋的感觉
    2020-12-16 11:44

    When using http.request(), you have to at some point call request.end().

    req = http.request options, (res) ->
        # ...
    
    req.on 'error', # ...
    
    req.end() # <---
    

    Until then, the request is left open to allow for writing a body. And, the error is because the server will eventually consider the connection to have timed out and will close it.

    Alternatively, you can also use http.get() with GET requests, which will call .end() automatically since GET requests aren't normally expected to have a body.

提交回复
热议问题