Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
925 views
in Technique[技术] by (71.8m points)

node.js - "Socket hang up" error during request

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 = 
        host: 'www.ya.ru'
        method: 'GET'
    req = http.request options, (res) ->
        output = ''
        console.log 'STATUS: ' + res.statusCode
        res.on 'data', (chunk) ->
            console.log 'A new chunk: ', chunk
            output += chunk

        res.on 'end', () ->
            console.log output
            console.log 'End GET Request'

    req.on 'error', (err) ->
        console.log 'Error: ', err
    req.end()

I get the following error during this operation: { [Error: socket hang up] code: 'ECONNRESET' }. If I comment the error handler my application is finished with the following error:

events.js:48
    throw arguments[1]; // Unhandled 'error' event
    ^
Error: socket hang up
    at createHangUpError (http.js:1091:15)
    at Socket.onend (http.js:1154:27)
    at TCP.onread (net.js:363:26)

I try to find out solution on the internet but still hasn't found them. How to solve this issue?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You have to end the request. Add this at the end of your script:

req.end()

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...