这个问题与Serverless offline not getting route 几乎相同,但由于没有回答我再次询问。我正在尝试关注这篇文章,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 comment fixed my issue .
您可以运行 $ sls offline start --noPrependStageInUrl
或将以下内容添加到您的 serverless.yml
文件
custom:
serverless-offline:
noPrependStageInUrl: true
根据评论:
I had this problem with anything 6+, this was due to the fact that it now defaults to appending the staging name to the url path. To revert to the old way, you need to add --noPrependStageInUrl to the cli or in the serverless file custom: serverless-offline noPrependStageInUrl: true to revert to previous setting. I'm testing it his out but @dherault the functionality is not reflecting what is actually happening in 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+}"
抱歉,这不是一个很好的答案,但希望有人会遇到这个问题,这是解决他们问题的方法。
关于javascript - "Serverless-offline: route not found."在离线模式下运行 AWS Lambda 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57733581/