我正在尝试使用NodeJS、AWS Lambda、API网关、RDS和PostgreSQL部署无服务器REST API。
到目前为止,我已经成功地设置了PostgreSQL RDS,在开始编写处理数据库请求的函数之前,我认为最好先在本地测试一个小函数,以检查请求是否得到了正确处理。
因此,在项目的根目录中,我离线安装了无服务器:
npm安装无服务器离线
它在安装类型的过程中抛出了几个警告
npm警告已弃用@hapi/pez@4.1.2:此版本已被弃用,不再支持或维护
(如果这些信息无关紧要,我很抱歉,我是新手,不知道什么是重要的,什么不是。(
然后我配置了无服务器。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
这是处理程序.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中的路径更改为";路径:/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上解决的问题让人很难理解。
有人能帮我吗?
如果升级到serverless或更高版本的2.41.2,此问题就会得到解决。
npm i -g serverless@2.41.2
我在尝试使用不同的无服务器插件(serverless-domain-manager
(时遇到了Cannot read property 'options' of undefined
错误。将Serverless版本降低到2.38或更早版本似乎可以解决这个问题。
遇到了同样的问题,但在切换到以前项目的package-lock.json文件(相同的package.json(后,问题得到了解决。所以我认为是一个依赖导致了这个问题,但很抱歉,我还不能确定这个依赖是什么