对于AWS lambda无服务器nodejs应用程序,如何在开发环境中执行请求



我正在接管一个AWS lambda无服务器nodejs应用程序,并试图弄清楚如何在我的开发环境中执行端点。出于我的目的,我试图模拟如下请求:

http://localhost:3000/find/product?hiveProductId=22002233&zipCode=44035

该应用程序在handler.js 中包含以下内容

app.route("/find/product").get(cors(), async (req, res) => {
try {
console.log(
"finding product",
req.query
);
etc...
} catch (error) {
console.error("caught error finding product: ", error);
res.status(500).json(error);
}
});
module.exports.find = serverless(app);

serverless.yml中还有以下内容:

functions:
find:
handler: handler.find
events:
- http:
path: /find/product
method: GET
memorySize: 128
timeout: 30
private: false
cors: true
integration: lambda-proxy
request:
parameters:
querystrings:
hiveProductId: true 
max: false
lat: false
lon: false
allowGeoIp: false
zipCode: false
methodReponses:
- statusCode: "200"
responseBody:
description: "array of stores with product"
responseModels:
"application/json": "Stores"
- statusCode: "404"
description: "not found"
- http:
path: /find/stores
method: GET
memorySize: 128
timeout: 30
integration: lambda-proxy
private: true
request:
parameters:
querystrings:
max: false
lat: true
lon: true
documentation:
summary: "Find the closest stores"
queryParams:
- name: "lat"
description: "latitude caller for geosearch"
required: true
- name: "lon"
description: "longtitude caller for geosearch"
required: true
- name: "max"
description: "maximum stores to location to return. default is 5"
methodReponses:
- statusCode: "200"
responseBody:
description: "array of stores sorted by distance"
responseModels:
"application/json": "Stores"

我一直在使用https://www.serverless.com/framework/docs/providers/aws/cli-reference/invoke-local/以供参考。serverless invoke local似乎是我想要的。serverless invoke local --function find给出以下响应:

{
"statusCode": 404,
"headers": {
"x-powered-by": "Express",
"content-security-policy": "default-src 'none'",
"x-content-type-options": "nosniff",
"content-type": "text/html; charset=utf-8",
"content-length": "139"
},
"isBase64Encoded": false,
"body": "<!DOCTYPE html>n<html lang="en">n<head>n<meta charset="utf-8">n<title>Error</title>n</head>n<body>n<pre>Cannot GET /</pre>n</body>n</html>n"
}

任何关于如何正确使用serverless invoke的建议或指针,或不同的研究方法,或任何更有成效的文档,都将不胜感激。

我以前没有用过这个方法,所以我什么都说不出来。我可以推荐无服务器离线作为另一种解决方案。无服务器离线

您正在询问如何测试和调试无服务器应用程序。。这是我为它使用的.vscode/launch.json(放在VS代码项目的根目录中((我总是使用VS代码(:

{
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "launch",
"name": "Launch Lambda",
"runtimeExecutable": "/Users/ninjablade/.nvm/versions/node/v12.18.0/bin/node",
"cwd": "${workspaceFolder}/lambda-mock",
"program": "/usr/local/bin/sls",
"args": [
"invoke",
"local",
"-f",
"resolvers",
"-p",
"../mocks/resolvers.json"
],
"skipFiles": ["<node_internals>/**"]
},
]
}

正如您在这里所注意到的,我们正在运行$ sls invoke local -f function -p data命令(您可以从sls框架文档中查看该命令的确切指南(

作为该命令的-p选项,我们可以传递端点所需的任何信息。您可以轻松地从Cloud Watch日志中获取测试事件数据,并将其替换为测试输入数据。

$ sls invoke local命令将在本地开发环境中模拟完全相同的lambda环境,并允许您在本地中测试lambda函数。

这篇文章将帮助你通过VS代码调试无服务器应用程序。感谢

最新更新