这个问题与Serverless offline not getting route几乎相同,但是由于没有回答,所以我再次询问。我正在尝试阅读https://medium.com/@awesome1888/how-to-use-serverless-locally-with-webpack-and-docker-5e268f71715,以了解如何使用Serverless部署Lambda函数。

我有一个具有以下结构的目录:

> tree -I node_modules
.
├── package-lock.json
├── package.json
├── serverless.yml
├── src
│   ├── handler.js
│   └── index.js
└── webpack.config.js
serverless.yml读取的位置
service: my-first-lambda

plugins:
  - serverless-webpack
  - serverless-offline

provider:
  name: aws
  runtime: nodejs10.x
  region: us-east-1
  stage: dev

functions:
  hello:
    handler: src/handler.main
    events:
      - http:
        path: /hello
        method: any

custom:
  webpack:
    includeModules: true
src/index.js读取
import moment from 'moment';

const handler = async (event, context) => {
  const body = await new Promise((resolve) => {
    setTimeout(() => {
      resolve(`Hello, this is your lambda speaking. Today is ${moment().format('dddd')}`)
    }, 2000);
  });
  return {
    statusCode: 200,
    body,
  };
}

export default handler;
src/handler.js读取
export { default as main } from './index';

webpack.config.js读取
const path = require("path");
const nodeExternals = require("webpack-node-externals");
const slsw = require("serverless-webpack");

module.exports = {
  entry: slsw.lib.entries,
  target: "node",
  mode: slsw.lib.webpack.isLocal ? "development" : "production",
  externals: [nodeExternals()],
  output: {
    libraryTarget: "commonjs",
    path: path.join(__dirname, ".webpack"),
    filename: "[name].js"
  },
  module: {
    rules: [
      {
        test: /\.js$/,
        use: [
          {
            loader: "babel-loader",
            options: {
              presets: ["@babel/preset-env"],
              plugins: ["@babel/plugin-proposal-object-rest-spread"]
            }
          }
        ]
      }
    ]
  }
};

问题是当我以离线模式启动该功能时,似乎只有一条非常具体的路线:
>
npx serverless offline start --region us-east-1 --noTimeout --port 3000 --host 0.0.0.0
Serverless: Bundling with Webpack...
Time: 1203ms
Built at: 08/30/2019 2:35:10 PM
         Asset      Size       Chunks             Chunk Names
src/handler.js  6.81 KiB  src/handler  [emitted]  src/handler
Entrypoint src/handler = src/handler.js
[./src/handler.js] 42 bytes {src/handler} [built]
[./src/index.js] 1.64 KiB {src/handler} [built]
[moment] external "moment" 42 bytes {src/handler} [built]
Serverless: Watching for changes...
Serverless: Starting Offline: dev/us-east-1.

Serverless: Routes for hello:
Serverless: POST /{apiVersion}/functions/my-first-lambda-dev-hello/invocations

Serverless: Offline [HTTP] listening on http://0.0.0.0:3000
Serverless: Enter "rp" to replay the last request

如果我转到http://localhost:3000/hello,则会收到以下响应:
{"statusCode":404,"error":"Serverless-offline: route not found.","currentRoute":"get - /hello","existingRoutes":["post - /{apiVersion}/functions/my-first-lambda-dev-hello/invocations"]}

知道为什么这不起作用吗? (我仔细阅读了https://serverless.com/framework/docs/,但无法快速找到答案)。

最佳答案

看来serverless.yml文件中出现空格问题。

尝试在path块下缩进methodhttp:

functions:
  hello:
    handler: src/handler.main
    events:
      - http:
          path: /hello
          method: any

关于javascript - “Serverless-offline: route not found.”在离线模式下运行AWS Lambda函数,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/57733581/

10-13 08:26