Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/34.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 如何获取Lambda函数以使用任何方法从ApiGateway获取httpmethod?_Node.js_Amazon Web Services_Aws Lambda_Aws Api Gateway - Fatal编程技术网

Node.js 如何获取Lambda函数以使用任何方法从ApiGateway获取httpmethod?

Node.js 如何获取Lambda函数以使用任何方法从ApiGateway获取httpmethod?,node.js,amazon-web-services,aws-lambda,aws-api-gateway,Node.js,Amazon Web Services,Aws Lambda,Aws Api Gateway,我正在尝试创建一个API网关,它将接受AWS中的任何方法。调用API后,lambda函数将解析发送的消息,并决定从那里执行什么操作。因此,给定API网关方法: Type: AWS::ApiGateway::Method Properties: RestApiId: !Ref myRestApi ResourceId: !Ref myResource HttpMethod: ANY AuthorizationType: NONE

我正在尝试创建一个API网关,它将接受AWS中的任何方法。调用API后,lambda函数将解析发送的消息,并决定从那里执行什么操作。因此,给定API网关方法:

Type: AWS::ApiGateway::Method
    Properties:
      RestApiId: !Ref myRestApi
      ResourceId: !Ref myResource
      HttpMethod: ANY
      AuthorizationType: NONE
      Integration:
        Type: AWS_PROXY
        IntegrationHttpMethod: POST
        Uri:
          Fn::Join:
          - ''
          - - 'arn:aws:apigateway:'
            - Ref: AWS::Region
            - :lambda:path/2015-04-30/functions/
            - Fn::GetAtt:
              - myLambdaFunction
              - Arn
            - /invocations
它将成功地调用
myLambdaFunction
,那么如何让节点get中的lambda函数实际发送
HttpMethod

例如:

exports.handler = (event, context, callback) => {
    const response = {
        statusCode: 200,
        headers: {
            "x-custom-header" : "This exists for reasons."
        }

    };

// I know that event doesn't actually have any httpmethod, but I'm not sure what it does have, or how to use it.
    switch(event.httpmethod) {
      case "POST":
        console.log("POST!!!");
        create(event, context, callback);
        break;
      case "GET":
        console.log("GET!!!");
        read(event, context, callback);
        break;
      case "PUT":
        console.log("PUT!!!");
        update(event, context, callback);
        break;
    }

上面的lambda应该能够使用console.log,不管它使用哪种方法,但我不确定应该用什么来代替我刚刚编写的
event.httpmethod

事件变量是一个给定了lambda获得的json的请求

要使代码正常工作,需要将以下json传递给lambda

{ 
    httpmethod : "value"
}
值将在哪里发布、获取或放置


如果转到按钮操作右侧的控制台,可以使用事件json输入创建测试。

您正在查找
事件.httpMethod
(注意大写
M
)属性

如果不确定Lambda事件包含哪些数据,则始终可以使用

console.log(event);
结果将显示在与Lambda函数关联的CloudWatch日志中

对于API Gateway和Lambda之间的代理集成,您可以在中找到有关这些事件的特定详细信息:

或在:


我已使用以下命令找到
httpMethod
值-

if (event.requestContext.http.method === 'GET') {
    // code goes here...
}

下面的代码可以用来查找方法

if (event.httpMethod === "GET") {
  // Get method code goes here
} else if(event.httpMethod === "POST") {
  // Post method code goes here
}
示例API网关代理事件(REST API)

if (event.requestContext.http.method === 'GET') {
    // code goes here...
}
if (event.httpMethod === "GET") {
  // Get method code goes here
} else if(event.httpMethod === "POST") {
  // Post method code goes here
}