Cor*_*ter 14 api amazon-web-services node.js aws-lambda
在用 Node.js 编写的 AWS lambda 中,当我通过 API 网关执行 GET 调用时,我想提取 URL 的以下部分:
/devices/{id} --> {id} will be replaced by a value, and that is the value I want!
Run Code Online (Sandbox Code Playgroud)
我知道要获得 QueryStringParameters 你只需使用
event.queryStringParameters.[parameter name]
Run Code Online (Sandbox Code Playgroud)
但是我将如何对路径参数执行此操作,例如上面的 {id}。
还有什么地方可以让我全面了解在 Node.js 中为 API 编写 lambda 表达式吗?
jsp*_*pru 15
简短的回答:
const { id } = event.pathParameters;
Run Code Online (Sandbox Code Playgroud)
我最近发布了一个简短的培训视频,详细演示了如何创建 API Gateway REST API 并将它们与 AWS Lambda (NodeJS) 集成。请在这里查看:
var*_*nit 14
我假设您在这里使用 lambda 代理,我正在粘贴 lambda 代理的事件对象示例。
{
"message": "Good day, John of Seattle. Happy Friday!",
"input": {
"resource": "/{proxy+}",
"path": "/Seattle",
"httpMethod": "POST",
"headers": {
"day": "Friday"
},
"queryStringParameters": {
"time": "morning"
},
"pathParameters": {
"proxy": "Seattle"
},
"stageVariables": null,
"requestContext": {
"path": "/{proxy+}",
"accountId": "123456789012",
"resourceId": "nl9h80",
"stage": "test-invoke-stage",
"requestId": "test-invoke-request",
"identity": {
"cognitoIdentityPoolId": null,
"accountId": "123456789012",
"cognitoIdentityId": null,
"caller": "AIDXXX...XXVJZG",
"apiKey": "test-invoke-api-key",
"sourceIp": "test-invoke-source-ip",
"accessKey": "ASIXXX...XXDQ5A",
"cognitoAuthenticationType": null,
"cognitoAuthenticationProvider": null,
"userArn": "arn:aws:iam::123456789012:user/kdeding",
"userAgent": "Apache-HttpClient/4.5.x (Java/1.8.0_131)",
"user": "AIDXXX...XXVJZG"
},
"resourcePath": "/{proxy+}",
"httpMethod": "POST",
"apiId": "r275xc9bmd"
},
"body": "{ \"callerName\": \"John\" }",
"isBase64Encoded": false
}
}
Run Code Online (Sandbox Code Playgroud)
路径可以从事件对象中的“path”键中提取,可以从 event.path 访问,之后您可以使用字符串操作函数进一步操作它。
我希望它有帮助!
Dan*_*iel 12
在资源路径中使用括号,如上图所示。然后在node.js用户中输入以下代码:
exports.handler = async function(event) {
let serviceId = event.pathParameters.id;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
24738 次 |
| 最近记录: |