AWS Lambda - 使用 Node.js 获取路径参数 [英] AWS Lambda - Getting path parameters using Node.js

查看:36
本文介绍了AWS Lambda - 使用 Node.js 获取路径参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在用 Node.js 编写的 AWS lambda 中,当我通过 API 网关执行 GET 调用时,我想提取 URL 的以下部分:

In an AWS lambda written in Node.js, I want to extract the following part of a URL when I do a GET call through the API gateway:

/devices/{id} --> {id} will be replaced by a value, and that is the value I want!

我知道要获取您刚刚使用的 QueryStringParameters

I know that to get QueryStringParameters you just use

event.queryStringParameters.[parameter name]

但是我将如何对路径参数执行此操作,例如上面的 {id}.

But how will I do this for path parameters, like for {id} above.

还有什么好地方可以让我全面了解在 Node.js 中为 API 编写 lambda 表达式吗?

Also is there a good place where I can comprehensively learn about writing lambdas for APIs in Node.js?

推荐答案

我假设您在此处使用 lambda 代理,我正在粘贴 lambda 代理的事件对象示例.

I'm assuming you are using lambda proxy here i'm pasting the event object sample for lambda proxy.

    {
  "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
  }
}

路径可以从事件对象中的path"键中提取,可以从event.path中访问,之后您可以使用字符串操作函数进一步操作它.

the path can be extracted from "path" key in event object, it can be accessed from event.path and after that you can use string manipulation function to further manipulate it.

希望能帮到你!

这篇关于AWS Lambda - 使用 Node.js 获取路径参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆