"Serverless-offline: route not found."离线模式下运行 AWS Lambda 函数



这个问题与无服务器离线无法获取路由几乎相同,但由于没有回答这个问题,我再次询问。https://medium.com/@awesome1888/how-to-use-serverless-locally-with-webpack-and-docker-5e268f71715,我正在尝试按照这篇文章了解如何使用无服务器部署 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/但无法快速找到答案(。

我遇到了这个问题,如果有人遇到它,这个 github 评论解决了我的问题。

您可以运行$ sls offline start --noPrependStageInUrl或将以下内容添加到serverless.yml文件中

custom:
serverless-offline:
noPrependStageInUrl: true

根据评论:

我对任何 6+ 都有这个问题,这是因为它现在默认将暂存名称附加到 url 路径。要恢复到旧方式,您需要将 --noPrependStageInUrl 添加到 cli 或无服务器文件自定义:serverless-offline noPrependStageInUrl: true 以恢复到以前的设置。我正在测试它,但@dherault功能并没有反映 AWS 中实际发生的事情。

我正在使用serverless-offline: "6.7.0",我的index.handler如下:

const serverless = require("serverless-http");
const express = require("express");
const app = express();
app.get("/", function (req, res) {
res.send("Hello World!");
});
module.exports.handler = serverless(app);

还有我的serverless.yml

plugins:
- serverless-offline
custom:
serverless-offline:
noPrependStageInUrl: true
provider:
name: aws
runtime: nodejs12.x
stage: dev
region: eu-west-2
functions:
app:
handler: src/index.handler
events:
- http: ANY /
- http: "ANY {proxy+}"

抱歉,这不是一个很好的答案,但希望有人遇到这个问题,这是他们问题的解决方案。

看起来您的serverless.yml文件中存在空格问题。

尝试缩进path并在http块下method

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

用于设置使用无服务器模板的快速示例:

sls create -h

输出:

create ........................ Create new Serverless service
--template / -t .................... Template for the service. Available templates: "aws-clojure-gradle", "aws-clojurescript-gradle", "aws-nodejs", "aws-nodejs-typescript", "aws-alexa-typescript", "aws-nodejs-ecma-script", "aws-python", "aws-python3", "aws-groovy-gradle", "aws-java-maven", "aws-java-gradle", "aws-kotlin-jvm-maven", "aws-kotlin-jvm-gradle", "aws-kotlin-nodejs-gradle", "aws-scala-sbt", "aws-csharp", "aws-fsharp", "aws-go", "aws-go-dep", "aws-go-mod", "aws-ruby", "aws-provided", "azure-nodejs", "cloudflare-workers", "cloudflare-workers-enterprise", "cloudflare-workers-rust", "fn-nodejs", "fn-go", "google-nodejs", "google-python", "google-go", "kubeless-python", "kubeless-nodejs", "openwhisk-java-maven", "openwhisk-nodejs", "openwhisk-php", "openwhisk-python", "openwhisk-ruby", "openwhisk-swift", "spotinst-nodejs", "spotinst-python", "spotinst-ruby", "spotinst-java8", "plugin" and "hello-world"

步骤1:所以对于生成一个新的NodeJS示例和一个API:

sls create -t aws-nodejs-ecma-script -n service-name-hello-world

步骤2:安装无服务器离线:

npm install serverless-offline -D

第 3 步:在无服务器中

plugins:
- serverless-webpack
- serverless-offline

步骤4:启动本地服务器

serverless offline start -r us-west-1 --stage dev

GitHub示例

git clone https://github.com/ysfmag/aws-serverless-nodejs-example
cd aws-serverless-nodejs-example
yarn 
yarn start 

serverless.yml

要在无服务器框架中定义API,您需要尊重YAML格式,并且在路径变量中,您不需要以"/Hello"开头,只需"hello"即可。

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

只是指出路径上方的方法与除 GET(facepalm(之外的所有其他方法的行为不同:

  • 未找到路由:

    - http:
    method: ANY
    path: /
    - http:
    method: ANY
    path: '{proxy+}'
    
  • 加工:

    - http:
    path: /
    method: ANY
    - http:
    path: '{proxy+}'
    method: ANY
    

相关内容

  • 没有找到相关文章

最新更新