2016-08-02 38 views
0

我试图处理从ASP.NET WEB API发送到AWS API并处理它与lambda函数请求从ASP.NET WEB API发送到AWS LAMBDA的JSON请求:如何处理如何使用Node.js

这就是我调用REST API部署在AWS方式:

// Serialize our concrete class into a JSON String 
var stringPayload = await Task.Run(() => JsonConvert.SerializeObject(payload)); 
// Wrap our JSON inside a StringContent which then can be used by the HttpClient class 
var httpContent = new StringContent(stringPayload,Encoding.UTF8,"application/json"); 
using (var httpClient = new HttpClient()) { 
    // Do the actual request and await the response 
    var httpResponse = await httpClient.PostAsync("https://hosteddress-2.amazonaws.com/prod/SlashcmdIntegeration", httpContent); 
    // If the response contains content we want to read it! 
    if (httpResponse.Content != null) { 
     var responseContent = await httpResponse.Content.ReadAsStringAsync(); 
     // From here on you could deserialize the ResponseContent back again to a concrete C# type using Json.Net 
    } 
}      

这是代码AWS lambda来处理它:

var AWS = require('aws-sdk'); 
var qs = require('querystring'); 
var https=require('https'); 
var token; 
exports.handler = function (event, context) { 
    processEvent(event, context); 
}; 

var processEvent = function(event, context) { 
    var body = event.body; 
    var params = qs.parse(body); 
    var commandText = params.CommandText; 
    var arr = commandText.split(" "); 
    var op1 = arr[0]; 
    var op2 = arr[1]; 
    var op = arr[2]; 
    var result; 
    switch(op) { 
     case "+": 
      result = Number(op1) + Number(op2); 
      break; 
     case "-": 
      result = Number(op1) - Number(op2); 
      break; 
     case "*": 
      result = Number(op1) * Number(op2); 
      break; 
     case "/": 
      result = Number(op2)===0 ? NaN : Number(op1)/Number(op2); 
      break; 
     default: 
      result = "Invalid op"; 
    } 
    console.log('data sent'); 
    context.succeed(result); 
}; 

我试图调试它var body = event.body;是不确定的,但是我已经在合并请求设置,并最终登陆以下错误信息:

{“的errorMessage”:“在完成请求之前处理退出”}

对此有何想法请?

感谢

+0

没有得到这个'var body = event.body;它不是未定义的“,你是否在请求中获得了正文? – AJS

+0

@AJS。这就是我认为的问题,event.body是未定义的,但它应该获得正确的身体,因为我已经在AWS API的集成请求中正确设置了身体。 – TechnoSavvy

+0

错误意味着api请求失败,因为它没有发送任何响应。 – AJS

回答

0

从我可以从上面的代码中了解

当你的过程是无法出现此错误{ “的errorMessage” “进程完成请求之前退出”}发送一个响应,因为它可以这样做之前发生了一些错误。我建议你在开始解析它之前检查它是否是这样的

var processEvent = function(event, context) { 
 
    var body = event.body; 
 
    var result; 
 
    if (body) { 
 

 
    var params = qs.parse(body); 
 
    var commandText = params.CommandText; 
 
    var arr = commandText.split(" "); 
 
    var op1 = arr[0]; 
 
    var op2 = arr[1]; 
 
    var op = arr[2]; 
 

 
    switch (op) { 
 
     case "+": 
 
     result = Number(op1) + Number(op2); 
 
     break; 
 
     case "-": 
 
     result = Number(op1) - Number(op2); 
 
     break; 
 
     case "*": 
 
     result = Number(op1) * Number(op2); 
 
     break; 
 
     case "/": 
 
     result = Number(op2) === 0 ? NaN : Number(op1)/Number(op2); 
 
     break; 
 
     default: 
 
     result = "Invalid op"; 
 
    } 
 
    } 
 

 
    console.log('data sent'); 
 
    context.succeed(result); 
 
};

至于是什么原因造成我不太清楚,因为我没有使用拉姆达的错误,这纯粹是从的的NodeJS观点。

+0

我会更新答案,如果我发现任何特定有用的信息,可以帮助在API的集成请求lamda – AJS

+0

我已经给身体映射模板为:{“body”:$ input.json('$')},事件不接受它在node.js处理程序。我刚刚尝试过直接调用像事件。[参数名称]正在工作:) – TechnoSavvy

+0

我发现这是一个很好,干净的实现可能会尝试this.https:// aws.amazon.com/blogs/compute/how-to-turn-node-js-projects-into-aws-lambda-microservices-easily-with-claudiajs/ – AJS

0

直接从事件调用参数工作:

var processEvent = function(event, context) { 
     var body = event.CommandText;/*parameter name*/ 
     var result; 
     if (body) { 


     var arr = body.split(" "); 
     var op1 = arr[0]; 
     var op2 = arr[1]; 
     var op = arr[2]; 

     switch (op) { 
      case "+": 
      result = Number(op1) + Number(op2); 
      break; 
      case "-": 
      result = Number(op1) - Number(op2); 
      break; 
      case "*": 
      result = Number(op1) * Number(op2); 
      break; 
      case "/": 
      result = Number(op2) === 0 ? NaN : Number(op1)/Number(op2); 
      break; 
      default: 
      result = "Invalid op"; 
     } 
     } 

     console.log('data sent'); 
     context.succeed(result); 
    }; 

只是想知道关于使用人体绘图模板。