简体   繁体   English

serverless-offline 可选路径参数

[英]serverless-offline optional path parameter

I'm trying to set up a GET request with an optional parameter but I get an error when I call the url locally without the optional parameter.我正在尝试使用可选参数设置 GET 请求,但是当我在没有可选参数的情况下在本地调用 url 时出现错误。 It works fine online on lambda though.不过,它在 lambda 上在线运行良好。 What did I do wrong?我做错了什么?

I'm using serverless version 1.24.1 with the serverless-offline plugin version 3.16.0我正在使用无服务器版本 1.24.1 和无服务器离线插件版本 3.16.0

here is my request definition in serverless.yml :这是我在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

this url works:这个 url 工作:

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

this don't这不

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

and gives me this output给我这个 output

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

Thanks a lot非常感谢

Add "?" 添加“?” after the params can make it work. 在params之后可以使它工作。

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

Unfortunately Chen Dachao's answer fails with: 不幸的是陈大超的回答失败了:

An error occurred: ApiGatewayResourceExperimentExperimentVarPsizeVar - Resource's path part only allow a-zA-Z0-9._- and curly braces at the beginning and the end. 发生错误:ApiGatewayResourceExperimentExperimentVarPsizeVar - 资源的路径部分仅允许a-zA-Z0-9 ._-和开头和结尾的花括号。

The current workaround to this is adding http handlers for each 'optional' variable in the path like so: 目前的解决方法是为路径中的每个“可选”变量添加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

If you want itemstatus to be optional then you must set it to false in your serverless request definition like so: 如果您希望itemstatus是可选的,那么您必须在无服务器请求定义中将其设置为false,如下所示:

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

I've used the following option and it worked with and without the parameters我使用了以下选项,它使用和不使用参数

- http:
      path: auth/{role?}
      method: get
      request:
          parameter:
              paths:
                  role: false

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法获取路径:/{proxy+} 工作(无服务器离线) - Cant get path: /{proxy+} to work (serverless-offline) 无服务器离线 - API REST 节点 typescript - Serverless-offline - API REST node typescript 带有无服务器离线阶段中断路由的 aws-serverless-express - aws-serverless-express with serverless-offline stage breaks routing 如何在 function 之前在 mocha 测试中启动 serverless-offline - How to start serverless-offline in mocha test before function 在本地 docker localhost:8000 中使用 dynamodb 和在 localhost:4500 上运行的 serverless-framework serverless-offline 应用程序 - Using dynamodb in local docker localhost:8000 with serverless-framework serverless-offline application running on localhost:4500 如何在无服务器离线中使用 Cognito 事件/触发器调用 lambda 以进行本地测试 - How to invoke lambda with Cognito event/trigger in serverless-offline for local test Serverless-offline 抛出“配置错误”或“无法读取未定义的属性‘选项’” - Serverless-offline throws "Configuration error" or "Cannot read property 'options' of undefined" 如何摆脱包装无服务器离线 html 响应正文的双引号? - How to get rid of double quotes wrapping serverless-offline html response body? 未找到无服务器命令“离线” - Serverless command "offline" not found 无服务器 - 离线插件 - Serverless - offline plugin
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM