【问题标题】:Serverless offline not getting route无服务器离线没有获得路由
【发布时间】:2019-03-12 21:18:49
【问题描述】:

我有一个刚刚初始化的无服务器项目:

severless.yml:

service: lambda

provider:
  name: aws
  runtime: nodejs8.10
  region: us-east-1

functions:
  hello:
    handler: handler.hello
    events:
      - http:
          path: /hello
          method: GET

plugins:
  - serverless-offline

我用sls offline --port 8080运行它:

Serverless: Starting Offline: dev/us-east-1

Serverless: Routes for hello:
Serverless: GET /hello

Serverless: Offline listening on http://localhost:8080

在我的handler.ts

module.exports.hello = async (event, context) => {
  console.log('HIIII')
  return {
    statusCode: 200,
    body: JSON.stringify({
      message: 'Go Serverless v1.0! Your function executed successfully!',
      input: event,
    }),
  };
};

我在本地在 Postman 上点击了这个端点,但它似乎不起作用。我得到的只是一些 HTML,上面写着Cannot GET /hello。我尝试向http://localhost:8080/dev/hellohttp://localhost:8080/hello 发出GET 请求,但我仍然遇到同样的错误。我在这里做错了吗?

【问题讨论】:

  • 你确定吗?我只是复制/粘贴了您示例的每个部分,http://localhost:8080/hello 工作得很好。
  • @Spacebear5000 你提到了 handler.ts,你确定你在 serverless.yml 上引用了正确的 js 文件吗?
  • 可能与您的回调有关。检查它是否正在返回某些内容,并且根据您用于发出请求的工具,它可能无法识别响应。
  • 您很可能需要重新启动“无服务器离线”命令。

标签: serverless-framework serverless


【解决方案1】:

您需要先构建您的 typescript 项目,serverless-offline 才能运行您的代码。 您可以创建一个tsconfig.json 文件并在运行serverless offline start 之前构建项目,或者您可以使用serverless-plugin-typescript 为您执行此操作。只需确保插件在serverless.yml 文件中的serverless-offline 插件上方声明即可。

【讨论】:

    【解决方案2】:

    我遇到了同样的问题,请确保您没有在该端口上运行其他东西

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-01-04
      • 2014-05-28
      • 2019-07-22
      • 2013-03-18
      • 2022-11-03
      相关资源
      最近更新 更多