如何在nodejs中使用aws-sam start-api并将参数传递给处理程序?

gstyhher  于 12个月前  发布在  Node.js
关注(0)|答案(1)|浏览(67)

我有一个继承的项目,它在AWS中部署并成功运行,它由apigateway和nodejs组成。
到目前为止,整个管道需要通过完整部署来测试更改。我已经为launch.json创建了配置,这样我就可以通过vscode调试器来测试这些插件,它们都运行得很好。我的问题是处理程序获取变量的方式,因为它使用了commonjs:

exports handler = async (event) => {
    const params = event.params;

    ...usage of params
}

我无法将变量传递给API。我的设置如下。

launch.json

{
    "configurations": [
        "type": "aws-sam",
        "request": "direct-invoke",
        "name": "my-example:Function",
        "invokeTarget": {
            "target": "api",
            "templatePath": "template.yaml",
            "logicalId": "ApiFunction"
        },
        "lambda": {
            "runtime": "nodejs16.x"
        },
        "sam": {},
        "api": {
            "path": "/api",
            "httpMethod": "get"
        }
    ]
}

template.yaml

AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Resources:
  ApiFunction:
    Type: AWS::Serverless::Function
    Properties:
      Handler: index.handler
      Runtime: nodejs16.x
      Events:
        Post:
          Type: Api
          Properties:
            Path: '/api'
            Method: get

我在启动配置中尝试了querystring/payload等
有没有可能使用get或post方法,因为我不这么认为?

brccelvz

brccelvz1#

如果您使用的是start-api命令,那么它将作为标准API工作。对于GET请求,通常在路径中传递参数(my.domain.com/path_parameter_1/path_parameter_2&query_parameter1=value)如果您正在执行POST或PUT,则数据应包含在主体中。
您也可以使用invoke函数并将json文档作为事件传递。sam local invoke -n function_name -e event.json

相关问题