简体   繁体   中英

Knox S3 - NodeJS/ExpressJS Error: socket hang up

I encountered the error below with NodeJS module for Amazon S3: Knox

events.js:72
        throw er; // Unhandled 'error' event
              ^
Error: socket hang up
    at SecurePair.error (tls.js:934:23)
    at CleartextStream.read [as _read] (tls.js:432:17)
    at CleartextStream.Readable.read (_stream_readable.js:320:10)
    at EncryptedStream.write [as _write] (tls.js:345:25)
    at doWrite (_stream_writable.js:219:10)
    at writeOrBuffer (_stream_writable.js:209:5)
    at EncryptedStream.Writable.write (_stream_writable.js:180:11)
    at write (_stream_readable.js:573:24)
    at flow (_stream_readable.js:582:7)
    at Socket.pipeOnReadable (_stream_readable.js:614:5)
    at Socket.EventEmitter.emit (events.js:92:17)

After enabling longjohn , I can say that the error is on route for display image from Amazon S3.

exports.image = function(req, res) {
    var type = req.params.type;
    var id = req.params.id;
    var file = req.params.file;
    var url = '/' + type + '/' + id + '/' + file;


    var data = '';
    knoxClient.get(url).on('response', function(s3res) {
        s3res.setEncoding('binary');
        s3res.on('data', function(chunk){
            data += chunk;
        });
        s3res.on('end', function() {
            res.write(data, encoding='binary');
            res.end();
        });
    }).end();
};

How to handle the error such that the server will not crash ?

You most likely forgot to add a handler to the 'error' event of your server socket.

The reason why the stack does not contain any reference to your code is because of the evented nature of node.js. Whenever an event fires, the stack is restarted from scratch. Because of this, it's a bit hard to debug async calls.

You can try using longjohn during development.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM