【问题标题】:serverless-offline optional path parameterserverless-offline 可选路径参数
【发布时间】:2018-05-13 10:10:40
【问题描述】:

我正在尝试使用可选参数设置 GET 请求,但是当我在没有可选参数的情况下调用 本地 url 时出现错误。不过,它在 lambda 上运行良好。 我做错了什么?

我正在使用无服务器版本 1.24.1 和无服务器离线插件版本 3.16.0

这是我在 serverless.yml 中的请求定义:

functions:
getitems:
    handler: lambda.handler
    events:
      - http:
            path: item/store/{storeid}/{itemstatus}
            method: get
            cors: true
            request:
                parameters:
                  paths:
                    storeid: true
                    itemstatus: false

这个网址有效:

http://localhost:3000/item/store/123456/used

这不是

http://localhost:3000/item/store/123456

然后给我这个输出

{
   statusCode: 404,
   error: "Serverless-offline: route not found.",
   currentRoute: "get - /item/store/123456",
   existingRoutes: [
       "get - item/store/{storeid}/{itemstatus}"
   ]
}

非常感谢

【问题讨论】:

    标签: node.js serverless-framework serverless-framework-offline


    【解决方案1】:

    很遗憾陈大超的回答失败了:

    发生错误:ApiGatewayResourceExperimentExperimentVarPsizeVar - 资源的路径部分只允许 a-zA-Z0-9._- 和大括号 开头和结尾。

    目前的解决方法是为路径中的每个“可选”变量添加 http 处理程序,如下所示:

    functions:
      getitems:
        handler: lambda.handler
          events:
            - http:
                path: item/store/{storeid}
                method: get
                cors: true
                request:
                  parameter:
                    storeid: true
            - http:
                path: item/store/{storeid}/{itemstaus}
                method: get
                cors: true
                request:
                  parameter:
                    storeid: true
                    itemstatus: true
    

    【讨论】:

      【解决方案2】:

      添加“?”在参数可以使其工作之后。

      functions:
        getitems:
        handler: lambda.handler
        events:
          - http:
              path: item/store/{storeid}/{itemstatus?}
              method: get
              cors: true
      

      【讨论】:

      • 这仅适用于 1 个可选路径参数
      【解决方案3】:

      如果您希望 itemstatus 是可选的,那么您必须在无服务器请求定义中将其设置为 false,如下所示:

      - http:
                  path: item/store/{storeid}/{itemstaus}
                  method: get
                  cors: true
                  request:
                    parameter:
                      storeid: true
                      itemstatus: false
      

      【讨论】:

      • 部署错误:资源的路径部分只允许 a-zA-Z0-9._- 和开头和结尾的花括号。 (服务:AmazonApiGateway;状态代码:400;错误代码:BadRequestException
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-04-25
      • 1970-01-01
      • 1970-01-01
      • 2020-03-27
      • 2020-12-17
      • 2022-01-17
      • 1970-01-01
      相关资源
      最近更新 更多