Web 无服务器体系结构服务模式路径参数

Web 无服务器体系结构服务模式路径参数,web,serverless-framework,serverless,Web,Serverless Framework,Serverless,我是在读这篇关于无服务器体系结构中存在的不同模式的博文 我对服务模式感兴趣,我想试试 在我的serverless.yml文件中使用此配置 functions: apps: handler: handler.apps events: - http: post apps cors: true - http: patch users - http: get users cors: true - http

我是在读这篇关于无服务器体系结构中存在的不同模式的博文

我对
服务模式感兴趣,我想试试

在我的
serverless.yml
文件中使用此配置

functions:
  apps:
    handler: handler.apps
    events:
      - http: post apps
        cors: true
      - http: patch users
      - http: get users
        cors: true
      - http: delete users
以下输出来自运行
serverless deploy

POST - https://x7lpwa04.execute-api.us-west-2.amazonaws.com/staging/users
PATCH - https://x7lpwa04.execute-api.us-west-2.amazonaws.com/staging/users
GET - https://x7lpwa04.execute-api.us-west-2.amazonaws.com/staging/users
DELETE - https://x7lpwa04.execute-api.us-west-2.amazonaws.com/staging/users
现在,在CRUD服务中,如果我想获得单个资源,我可能会为
get
端点
/staging/users/{id}
提供类似的功能。在上面的模式中,是否由用户传入这样的查询字符串参数
/staging/users?id={id}
,而不是像这样的路径参数
/staging/users/{id}
?是否可以使端点具有路径参数


路径似乎不能以这种方式覆盖。

您已经可以使用路径参数,例如:

{
  "myFunction": {
    "handler": "src/myFunction/index.index",
    "description": "Does awesome things",
    "events": [
      {
        "http": {
          "path": "apiPath/{parameterOne}",
          "method": "GET",
          "integration": "lambda",
          "request": {
            "parameters": {
              "parameterOne": true
            },
            "template": {
              "application/json": "{ \"parameterOne\": \"$input.params(\"parameterOne\")\" }"
            }
          },
          "response": {
            "statusCodes": {
              "200": {
                "pattern": ""
              },
              "400": {
                "pattern": "[\\s\\S]*\\[400\\][\\s\\S]*",
                "template": "$input.path('$.errorMessage')"
              },
              "500": {
                "pattern": "[\\s\\S]*(Process\\s?exited\\s?before\\s?completing\\s?request|\\[500\\])[\\s\\S]*",
                "template": "$input.path('$.errorMessage')"
              }
            },
            "headers": {
              "Cache-Control": "'no-cache, no-store'",
              "Pragma": "'no-cache'",
              "Expires": "'0'",
              "Strict-Transport-Security": "'max-age=31536000; includeSubdomains; preload'"
            }
          },
          "cors": {
            "origin": "*",
            "headers": [
              "Content-Type",
              "Pragma",
              "Cache-Control",
              "X-Amz-Date",
              "Authorization",
              "X-Api-Key",
              "X-Amz-Security-Token",
              "X-Amz-User-Agent"
            ],
            "allowCredentials": false
          }
        }
      }
    ]
  }
}

参数
parameterOne
将被映射到Lambda事件中。

我可能解释错了,但至少在aws的情况下,您可以将多个资源添加到Lambda中。您将负责处理正确的行为,这可以通过解析事件来完成,如果路径参数存在,事件将填充路径参数

正如在博客上提到的

您可以通过解析代码中的事件体来检查传入HTTP请求的路径和方法,然后在响应中执行正确的操作。这就像在Lambda代码的开头有一个小路由器

您可以使用

  - http:
      path: users/{id}
      method: delete