问题描述
我正在尝试使用 NodeJS、AWS Lambda、API Gateway、RDS 和 Postgresql 部署无服务器 REST API。
到目前为止,我已经成功设置了 Postgresql RDS,在开始编写函数来处理对数据库的请求之前,我认为最好先在本地测试一个小函数以检查请求是否正在正确处理。
所以在项目的根目录下,我安装了 serverless-offline:
npm install serverless-offline
它在安装类型的过程中抛出了几个警告:
npm WARN deprecated @hapi/pez@4.1.2:此版本已弃用,不再支持或维护
(如果这些信息无关紧要,我很抱歉,我是新手,不知道什么是重要的,什么不重要。)
然后我配置了我的serverless.yml:
service: serverless-node-postgres-rds-rest-api
app: serverless-app
frameworkVersion: '2'
provider:
name: aws
runtime: nodejs12.x
lambdaHashingVersion: 20201221
plugins:
- serverless-offline
configValidationMode: error
functions:
hello:
handler: handler.hello
events:
- httpApi:
path: hello
method: get
这里是 handler.js:
'use strict';
module.exports.hello = async (event) => {
return {
statusCode: 200,body: JSON.stringify(
{
message: 'Go Serverless v1.0! Your function executed successfully!',input: event,},null,2
),};
// Use this code if you don't use the http event with the LAMBDA-PROXY integration
// return { message: 'Go Serverless v1.0! Your function executed successfully!',event };
};
运行时出现问题
无服务器离线
当它抛出错误时:
Serverless: Running "serverless" installed locally (in service node_modules)
Serverless Error ----------------------------------------
Configuration error at 'functions.hello.events[0].httpApi.path': value 'hello' does not satisfy pattern /^(?:\*|\/\S*)$/
Get Support --------------------------------------------
Docs: docs.serverless.com
Bugs: github.com/serverless/serverless/issues
Issues: forum.serverless.com
Your Environment information ---------------------------
Operating System: darwin
Node Version: 14.15.4
Framework Version: 2.40.0 (local)
Plugin Version: 4.5.3
SDK Version: 4.2.2
Components Version: 3.9.2
于是我将serverless.yml中的路径改为“path:/hello”,错误改为:
Type Error ----------------------------------------------
TypeError: Cannot read property 'options' of undefined
at module.exports (/Users/randresverap/Development/Node/serverless-node-postgres-rds-rest-api/node_modules/serverless/lib/utils/telemetry/generatePayload.js:133:66)
at async PluginManager.run (/Users/randresverap/Development/Node/serverless-node-postgres-rds-rest-api/node_modules/serverless/lib/classes/PluginManager.js:607:35)
at async Serverless.run (/Users/randresverap/Development/Node/serverless-node-postgres-rds-rest-api/node_modules/serverless/lib/Serverless.js:325:5)
at async /usr/local/lib/node_modules/serverless/scripts/serverless.js:634:9
For debugging logs,run again after setting the "SLS_DEBUG=*" environment variable.
如果我将路径更改为“路径:'*'”,它会抛出同样的最后一个错误。
我按照建议设置了“SLS_DEBUG=*”环境变量后再次运行它,但结果几乎相同,没有额外的调试信息。
谁能告诉我我做错了什么?我花了几个小时上网寻找解决方法,但我没有找到任何解决相同错误的帖子,而在 forum.serverless.com 上解决的问题提供了难以理解的纠缠信息。
有人可以帮我吗?
解决方法
我在尝试使用其他无服务器插件 (Cannot read property 'options' of undefined
) 时遇到了 serverless-domain-manager
错误。将 Serverless 版本降级到 2.38 或更早版本似乎可以解决此问题。
如果您升级到无服务器版本 2.41.2 或更高版本,则此问题已解决。
npm i -g serverless@2.41.2
遇到了同样的问题,但在从以前的项目切换到 package-lock.json 文件(相同的 package.json)后,问题得到了解决。因此,我认为存在导致此问题的依赖项,但很抱歉,我无法确定该依赖项是什么