本文介绍了“无服务器离线:找不到路由。”在离线模式下运行AWS Lambda函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此问题与几乎相同,但是由于没有人回答,我再次询问。我正在尝试阅读这篇文章,,介绍如何通过Serverless部署Lambda函数。

This question is pretty much the same as Serverless offline not getting route, but since that one was not answered I'm asking again. I'm trying to follow this article, https://medium.com/@awesome1888/how-to-use-serverless-locally-with-webpack-and-docker-5e268f71715, on how to deploy a Lambda function with Serverless.

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

I have a directory with the following structure:

> 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"]
            }
          }
        ]
      }
    ]
  }
};

问题是当我以离线模式启动该功能时,似乎只有一个非常具体的路线:

The problem is when I start up the function in offline mode, it seems to have only one very specific route:

> 
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

,如果我转到,我得到此响应:

and if I go to http://localhost:3000/hello, I get this response:

{"statusCode":404,"error":"Serverless-offline: route not found.","currentRoute":"get - /hello","existingRoutes":["post - /{apiVersion}/functions/my-first-lambda-dev-hello/invocations"]}

有人知道为什么这行不通吗? (我仔细阅读了但无法迅速找到答案。)

Any idea why this is not working? (I've perused https://serverless.com/framework/docs/ but couldn't quickly find an answer).

推荐答案

好像您的 serverless出现了空格问题。 yml 文件。

尝试缩进路径方法 http 块下:

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

这篇关于“无服务器离线:找不到路由。”在离线模式下运行AWS Lambda函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-11 07:26