无服务器离线:“路径”参数必须是字符串类型收到未定义

问题描述

不知道为什么会发生这种情况,但我有一个非常简单的无服务器应用程序可以正常工作,但是现在当我运行 sls offline start 时出现上述错误。我找到了罪魁祸首,它是函数内部的 events

这是 serverless.yml 文件

service: hello-world-offline

provider:
  name: aws
  runtime: nodejs12.x
  region: eu-east-1
  stage: dev

plugins:
  - serverless-offline

functions:
  hello-world:
    handler: handler.handle # required,handler set in AWS Lambda
    events:
      - http:
          path: hello-world
          method: get
          cors: true

这是 handler.js 文件

module.exports.handle = async (event,ctx,cb) => {
  cb(null,{
    statusCode: 200,body: JSON.stringify({ message: "hello world" })
  })
}

如果我去掉函数 events 中的 hello-world,一切都可以与 sls offline start 一起正常工作,当然我实际上无法在本地击中端点。我尝试通过添加引号使其成为硬字符串,但没有任何作用。

编辑:结果是使用 yarn workspaces 时会发生这种情况。如果我将它放在 packages/my-serverless-app 结构中,然后 cd 进入文件夹以运行 sls offline start 命令,就会发生这种情况。如果我将它从结构中移除,它就可以正常工作。

解决方法

改变

events:
      - http:
          path: hello-world
          method: get
          cors: true

events:
      - httpApi:
          path: hello-world
          method: get

这应该可行!