我正在尝试使用 NodeJS、AWS Lambda、API Gateway、RDS 和 PostgreSQL 部署无服务器 REST API。
到目前为止,我已经成功地设置了 PostgreSQL RDS,在开始编写函数来处理对 DB 的请求之前,我认为首先在本地测试一个小函数以检查请求是否被正确处理是一个好主意。
所以在项目的根目录下,我安装了 serverless-offline:
npm install serverless-offline
它在安装类型时抛出了几个警告:
npm WARN 已弃用@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 上解决的问题提供了难以理解的纠缠信息。
谁能帮我?