我正在尝试使用可选参数设置 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}"
]
}
非常感谢
最佳答案
不幸的是,陈大超的回答失败了:
An error occurred: ApiGatewayResourceExperimentExperimentVarPsizeVar - Resource's path part only allow a-zA-Z0-9._- and curly braces at the beginning and the end.
当前的解决方法是为路径中的每个“可选”变量添加 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
关于node.js - serverless-offline 可选路径参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47557222/