在无服务器环境中,我的功能具有以下目录结构:
serverless.yml
functions -
stories -
create.js
get.js
我的serverless.yml
如下所示:
functions:
stories:
create:
handler: functions/stories/create.main
events:
- http:
path: stories/create
method: post
cors: true
authorizer: aws_iam
get:
handler: functions/stories/get.main
events:
- http:
path: stories/{id}
method: get
cors: true
authorizer: aws_iam
但是,当我运行测试以检查创建情况时:serverless invoke local --function create --path mocks/create-event.json
我收到以下错误:
Serverless Error ---------------------------------------
Function "create" doesn't exist in this Service
我设法使一个功能看起来像这样:
functions:
stories:
handler: functions/stories/create.main
events:
- http:
path: stories/create
method: post
cors: true
authorizer: aws_iam
自从我添加了get函数以来,我决定我需要在故事后添加create和get part,但是无论我如何更改处理程序,这些函数似乎都不存在。
我尝试将路径更改为functions/stories/create/create.main
没什么区别,是否有明显的缺失,我不允许在同一位置使用多个处理程序?
我正在看下面的example,它使用了一个文件夹“ todos”,其中包含多个功能,但是我看不到它和我的文件夹之间有任何明显的区别,只是我添加了一个额外的文件夹
答案 0 :(得分:4)
您的模板无效。您不能只是将函数放在任意节点下以告诉框架它适用于您的应用程序的某些对象。您的stories:
节点应为注释。
尝试这样的事情:
functions:
# Stories related functions
createStory:
handler: functions/stories/create.main
events:
- http:
path: stories # You can post directly to stories to be more RESTful
method: post
cors: true
authorizer: aws_iam
getStory:
handler: functions/stories/get.main
events:
- http:
path: stories/{id}
method: get
cors: true
authorizer: aws_iam
# More examples to understand the routing
getAllStories:
handler: functions/stories/getAll.main # Returns a list of all stories
events:
- http:
path: stories
method: get
cors: true
authorizer: aws_iam
deleteStory:
handler: functions/stories/delete.main # Deletes a story
events:
- http:
path: stories/{id}
method: delete
cors: true
authorizer: aws_iam