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

查看:72
本文介绍了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天全站免登陆