【发布时间】:2016-05-07 03:53:48
【问题描述】:
我在使用 lambda 函数的 https 请求时遇到了一些问题。我收到以下错误:
{
"errorMessage": "socket hang up",
"errorType": "Error",
"stackTrace": [
"SecurePair.error (tls.js:1011:23)",
"EncryptedStream.CryptoStream._done (tls.js:703:22)",
"CleartextStream.read [as _read] (tls.js:499:24)",
"CleartextStream.Readable.read (_stream_readable.js:341:10)",
"EncryptedStream.onCryptoStreamFinish (tls.js:304:47)",
"EncryptedStream.g (events.js:180:16)",
"EncryptedStream.emit (events.js:117:20)",
"finishMaybe (_stream_writable.js:360:12)",
"endWritable (_stream_writable.js:367:3)",
"EncryptedStream.Writable.end (_stream_writable.js:345:5)"
]
}
来自以下代码。这段代码是在亚马逊的 AWS Lambda 服务中用 Nodejs 编写的。
var https = require('https');
var options = {
host: 'host.dynamic.com', // global IP goes here. I've tried a DDNS host and the address of my server
port: 8080,
method: 'POST',
headers: {
'Content-Type': 'text'
}
};
var data = 'OFF';
/**
* Pass the data to send as `event.data`, and the request options as
* `event.options`. For more information see the HTTPS module documentation
* at https://nodejs.org/api/https.html.
*
* Will succeed with the response body.
*/
exports.handler = function(event, context) {
var req = https.request(options, function(res) {
var body = '';
console.log('Status:', res.statusCode);
console.log('Headers:', JSON.stringify(res.headers));
res.setEncoding('utf8');
res.on('data', function(chunk) {
body += chunk;
});
res.on('end', function() {
console.log('Successfully processed HTTPS response');
// If we know it's JSON, parse it
if (res.headers['content-type'] === 'application/json') {
body = JSON.parse(body);
}
context.succeed(body);
});
});
console.log("Before the error");
req.on('error', context.fail);
req.write(data);
console.log("Before req.end()");
req.end();
};
任何帮助将不胜感激
【问题讨论】:
-
我也看到了同样错误的 SO 帖子,但由于我将它托管在 AWS Lambda 上,所以除了这里编写的代码之外,我没有太多控制权...... .
标签: node.js amazon-web-services aws-lambda