使用适用于 Node.js 的 Amazon 开发工具包设置边缘优化的 API - Amazon API Gateway
Amazon Web Services 文档中描述的 Amazon Web Services 服务或功能可能因区域而异。要查看适用于中国区域的差异,请参阅 中国的 Amazon Web Services 服务入门 (PDF)

本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。

使用适用于 Node.js 的 Amazon 开发工具包设置边缘优化的 API

举例来说,我们使用 Amazon 适用于 Node.js 的 SDK 来描述如何使用 Amazon SDK 创建 API Gateway API。有关使用 Amazon SDK 的更多信息,包括如何设置开发环境,请参阅 Amazon SDK

使用适用于 Node.js 的 Amazon 软件开发工具包设置 API 需要调用createRestApicreateResourcegetResourcesputMethodputMethodResponseputIntegration、和putIntegrationResponse函数。

以下过程将引导您完成使用这些 SDK 命令设置支持GET /petsGET /pets/{petId}方法的简单 PetStore API 的基本步骤。

使用适用于 Node.js 的 Amazon SDK 设置简单的 PetStore API
  1. 实例化开发工具包:

    var AWS = require('aws-sdk'); AWS.config.region = 'us-west-2'; var apig = new AWS.APIGateway({apiVersion: '2015/07/09'});
  2. 调用 createRestApi 函数来设置 RestApi 实体。

    apig.createRestApi({ name: "Simple PetStore (node.js SDK)", binaryMediaTypes: [ '*' ], description: "Demo API created using the Amazon SDK for node.js", version: "0.00.001" }, function(err, data){ if (!err) { console.log(data); } else { console.log('Create API failed:\n', err); } });

    该函数返回类似于以下结果的输出:

    { id: 'iuo308uaq7', name: 'PetStore (node.js SDK)', description: 'Demo API created using the Amazon SDK for node.js', createdDate: 2017-09-05T19:32:35.000Z, version: '0.00.001', binaryMediaTypes: [ '*' ] }

    生成的 API 标识符为 iuo308uaq7。您需要提供此标识符以继续设置 API。

  3. 调用 getResources 函数以检索 RestApi 的根资源标识符。

    apig.getResources({ restApiId: 'iuo308uaq7' }, function(err, data){ if (!err) { console.log(data); } else { console.log('Get the root resource failed:\n', err); } })

    该函数返回类似于以下结果的输出:

    { "items": [ { "path": "/", "id": "s4fb0trnk0" } ] }

    根资源标识符为 s4fb0trnk0。这是构建 API 资源树的起点,有了它才能执行下一步操作。

  4. 调用 createResource 函数以为 API 设置 /pets 资源,并在 s4fb0trnk0 属性上指定根资源标识符 (parentId)。

    apig.createResource({ restApiId: 'iuo308uaq7', parentId: 's4fb0trnk0', pathPart: 'pets' }, function(err, data){ if (!err) { console.log(data); } else { console.log("The '/pets' resource setup failed:\n", err); } })

    成功的结果如下:

    { "path": "/pets", "pathPart": "pets", "id": "8sxa2j", "parentId": "s4fb0trnk0'" }

    要设置 /pets/{petId} 资源,请调用以下 createResource 函数,并在 /pets 属性上指定新创建的 8sxa2j 资源 (parentId)。

    apig.createResource({ restApiId: 'iuo308uaq7', parentId: '8sxa2j', pathPart: '{petId}' }, function(err, data){ if (!err) { console.log(data); } else { console.log("The '/pets/{petId}' resource setup failed:\n", err); } })

    成功的结果会返回新创建的资源 id 值:

    { "path": "/pets/{petId}", "pathPart": "{petId}", "id": "au5df2", "parentId": "8sxa2j" }

    在此过程中,您通过指定 /pets 的资源 ID 来引用 8sxa2j 资源,通过指定 /pets/{petId} 的资源 ID 来引用 au5df2 资源。

  5. 调用 putMethod 函数为 GET 资源 (/pets) 添加 8sxa2j HTTP 方法。这将设置具有开放访问权限的 GET /pets Method

    apig.putMethod({ restApiId: 'iuo308uaq7', resourceId: '8sxa2j', httpMethod: 'GET', authorizationType: 'NONE' }, function(err, data){ if (!err) { console.log(data); } else { console.log("The 'GET /pets' method setup failed:\n", err); } })

    该函数返回类似于以下结果的输出:

    { "apiKeyRequired": false, "httpMethod": "GET", "authorizationType": "NONE" }

    要为 GET 资源 (/pets/{petId}) 添加 au5df2 HTTP 方法 (这会设置具有开放访问权限的 API 方法 GET /pets/{petId}),请按照如下所示调用 putMethod 函数。

    apig.putMethod({ restApiId: 'iuo308uaq7', resourceId: 'au5df2', httpMethod: 'GET', authorizationType: 'NONE', requestParameters: { "method.request.path.petId" : true } }, function(err, data){ if (!err) { console.log(data); } else { console.log("The 'GET /pets/{petId}' method setup failed:\n", err); } })

    该函数返回类似于以下结果的输出:

    { "apiKeyRequired": false, "httpMethod": "GET", "authorizationType": "NONE", "requestParameters": { "method.request.path.petId": true } }

    您需要按照上述示例所示设置 requestParameters 属性,以将客户端提供的 petId 值映射并传递到后端。

  6. 调用 putMethodResponse 函数以为 GET /pets 方法设置方法响应。

    apig.putMethodResponse({ restApiId: 'iuo308uaq7', resourceId: "8sxa2j", httpMethod: 'GET', statusCode: '200' }, function(err, data){ if (!err) { console.log(data); } else { console.log("Set up the 200 OK response for the 'GET /pets' method failed:\n", err); } })

    该函数返回类似于以下结果的输出:

    { "statusCode": "200" }

    要设置 GET /pets/{petId} 方法的 200 OK 响应,请调用 putMethodResponse 函数,并在 /pets/{petId} 属性上指定 au5df2 资源标识符 (resourceId)。

    apig.putMethodResponse({ restApiId: 'iuo308uaq7', resourceId: "au5df2", httpMethod: 'GET', statusCode: '200' }, function(err, data){ if (!err) { console.log(data); } else { console.log("Set up the 200 OK response for the 'GET /pets/{petId}' method failed:\n", err); } })
  7. 调用 putIntegration 函数以为 Integration 方法设置具有指定 HTTP 终端节点的 GET /pets,并在 /pets 属性上提供 8sxa2j 资源标识符 (parentId)。

    apig.putIntegration({ restApiId: 'iuo308uaq7', resourceId: '8sxa2j', httpMethod: 'GET', type: 'HTTP', integrationHttpMethod: 'GET', uri: 'http://perstore-demo-endpoint.execute-api.com/pets' }, function(err, data){ if (!err) { console.log(data); } else { console.log("Set up the integration of the 'GET /' method of the API failed:\n", err); } })

    该函数返回类似于以下内容的输出:

    { "httpMethod": "GET", "passthroughBehavior": "WHEN_NO_MATCH", "cacheKeyParameters": [], "type": "HTTP", "uri": "http://petstore-demo-endpoint.execute-api.com/petstore/pets", "cacheNamespace": "8sxa2j" }

    要设置 GET /pets/{petId} 方法与后端的 HTTP 终端节点 http://perstore-demo-endpoint.execute-api.com/pets/{id} 的集成,请调用以下 putIntegration 函数,并在 /pets/{petId} 属性上提供 API 的 au5df2 资源标识符 (parentId)。

    apig.putIntegration({ restApiId: 'iuo308uaq7', resourceId: 'au5df2', httpMethod: 'GET', type: 'HTTP', integrationHttpMethod: 'GET', uri: 'http://perstore-demo-endpoint.execute-api.com/pets/{id}', requestParameters: { "integration.request.path.id": "method.request.path.petId" } }, function(err, data){ if (!err) { console.log(data); } else { console.log("The 'GET /pets/{petId}' method integration setup failed:\n", err); } })

    该函数返回类似于以下内容的成功输出:

    { "httpMethod": "GET", "passthroughBehavior": "WHEN_NO_MATCH", "cacheKeyParameters": [], "type": "HTTP", "uri": "http://petstore-demo-endpoint.execute-api.com/petstore/pets/{id}", "cacheNamespace": "au5df2", "requestParameters": { "integration.request.path.id": "method.request.path.petId" } }
  8. 调用 putIntegrationResponse 函数为 GET /pets 方法设置 200 OK 集成响应,并在 /pets 属性上指定 API 的 8sxa2j 资源标识符 (resourceId)。

    apig.putIntegrationResponse({ restApiId: 'iuo308uaq7', resourceId: '8sxa2j', httpMethod: 'GET', statusCode: '200', selectionPattern: '' }, function(err, data){ if (!err) { console.log(data); } else console.log("The 'GET /pets' method integration response setup failed:\n", err); })

    该函数将返回类似于以下结果的输出:

    { "selectionPattern": "", "statusCode": "200" }

    要设置 GET /pets/{petId} 方法的 200 OK 集成响应,请调用 putIntegrationResponse 函数,并在 /pets/{petId} 属性上指定 API 的 au5df2 资源标识符 (resourceId)。

    apig.putIntegrationResponse({ restApiId: 'iuo308uaq7', resourceId: 'au5df2', httpMethod: 'GET', statusCode: '200', selectionPattern: '' }, function(err, data){ if (!err) { console.log(data); } else console.log("The 'GET /pets/{petId}' method integration response setup failed:\n", err); })
  9. 最好先测试调用 API,然后再进行部署。要测试调用 GET /pets 方法,请调用 testInvokeMethod 函数,并在 /pets 属性上指定 8sxa2j 资源标识符 (resourceId):

    apig.testInvokeMethod({ restApiId: 'iuo308uaq7', resourceId: '8sxa2j', httpMethod: "GET", pathWithQueryString: '/' }, function(err, data){ if (!err) { console.log(data) } else { console.log('Test-invoke-method on 'GET /pets' failed:\n', err); } })

    要测试调用 GET /pets/{petId} 方法,请调用 testInvokeMethod 函数,并在 /pets/{petId} 属性上指定 au5df2 资源标识符 (resourceId):

    apig.testInvokeMethod({ restApiId: 'iuo308uaq7', resourceId: 'au5df2', httpMethod: "GET", pathWithQueryString: '/' }, function(err, data){ if (!err) { console.log(data) } else { console.log('Test-invoke-method on 'GET /pets/{petId}' failed:\n', err); } })

  10. 最后,您可以部署 API 供您的客户调用。

    apig.createDeployment({ restApiId: 'iuo308uaq7', stageName: 'test', stageDescription: 'test deployment', description: 'API deployment' }, function(err, data){ if (err) { console.log('Deploying API failed:\n', err); } else { console.log("Deploying API succeeded\n", data); } })