NodeJs npm requestretry程序包不适用于AWS Lambda Nodejs8.10

时间:2018-12-14 21:02:13

标签: node.js aws-lambda

这是我的nodejs8.10测试代码:

var request = require('requestretry');
const testRequest = async() =>{
  request({
    url: 'https://jsonplaceholder.typicode.com/posts/1',
    json: true,        
    maxAttempts: 5,   // (default) try 5 times
    retryDelay: 5000,  // (default) wait for 5s before trying again
    retryStrategy: request.RetryStrategies.HTTPOrNetworkError 
  }, function(err, response, body){       
    if (response) {
        console.log('The number of request attempts: ' + response.attempts);
        console.log(body);
    }else{
         console.log('error: ', err);
    }
  });
}


testRequest(); // for local implementation

exports.handler = async (event) => {    
  let x = await testRequest();
  const response = {
    statusCode: 200,
    body: JSON.stringify('Hello from Test Lambda!'),
  };

  return response;
};

vs。 nodejs6.10测试代码:

var request = require('requestretry');

function testRequest () {
  request({
    url: 'https://jsonplaceholder.typicode.com/posts/1',
    json: true,
    // The below parameters are specific to request-retry
    maxAttempts: 5,   // (default) try 5 times
    retryDelay: 5000,  // (default) wait for 5s before trying again
    retryStrategy: request.RetryStrategies.HTTPOrNetworkError // (default) retry on 5xx or network errors
  }, function(err, response, body){
    // this callback will only be called when the request succeeded or after maxAttempts or on error
    if (response) {            
        console.log(body);
    }
  });
}

 testRequest();

 exports.handler = (event, context, callback) => {    
     testRequest();
     const response = {
       statusCode: 200,
       body: JSON.stringify('Hello from Lambda!'),
     };
    callback(null, response);
 };

它不会在lambda Nodejs 8.10上显示与requestretry包相关的日志,也不会显示任何错误消息。但是,它可以通过在node <filename>之后在本地计算机上运行命令npm install并获得必要的软件包(例如“ requestretry”及其必需的软件包)来获取响应和日志。如果上述文件转换为Nodejs6.10格式,并且在aws lambda和本地计算机上都没有问题。

在将代码从nodejs6.10(回调格式)转换为nodejs8.10(异步格式)之前,我需要找出原因。

0 个答案:

没有答案