gar*_*dni 0 javascript serverless-framework serverless aws-serverless
在无服务器环境中,我的功能具有以下目录结构:
serverless.yml
functions -
stories -
create.js
get.js
Run Code Online (Sandbox Code Playgroud)
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
Run Code Online (Sandbox Code Playgroud)
但是,当我运行测试以检查创建时:serverless invoke local --function create --path mocks/create-event.json我得到以下错误:
Serverless Error ---------------------------------------
Function "create" doesn't exist in this Service
Run Code Online (Sandbox Code Playgroud)
我设法使一个函数看起来像这样:
functions:
stories:
handler: functions/stories/create.main
events:
- http:
path: stories/create
method: post
cors: true
authorizer: aws_iam
Run Code Online (Sandbox Code Playgroud)
由于我添加了get函数,因此我决定需要添加create和故事后的部件,但是无论我如何更改处理程序,这些函数似乎都不存在。
我尝试将路径更改为functions/stories/create/create.main无差异,是否有明显的缺失,我不允许在同一位置使用多个处理程序?
我正在看下面的示例,该示例使用一个包含“ todos”文件夹的文件夹,其中包含多个功能,但是我看不到它和我的文件夹之间的任何明显区别,除非我添加了一个额外的文件夹。
Amr*_*Amr 10
花了这么多时间在这上面!刚刚发现当我输入下面的命令时,我提到了handle.js 文件中的函数名称,这是错误的!我应该调用 serverless.yml 文件中存在的处理程序名称本身
例如这是错误的:
sls invoke local --function testFunction
Run Code Online (Sandbox Code Playgroud)
这是对的:
sls invoke local --function test
Run Code Online (Sandbox Code Playgroud)
您的模板无效。您不能只是将函数放在任意节点下以告诉框架它适用于您的应用程序的某些对象。您的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
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1187 次 |
| 最近记录: |