我使用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)"
]
}
来自以下代码。此代码使用Amazon的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();
};
任何帮助将不胜感激
答案 0 :(得分:0)
我发现了问题。我正在与之通信的nodejs服务器要求'http'而不是'https'。