本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。
使用适用于 Node.js 的 Amazon 开发工具包设置边缘优化的 API
举例来说,我们使用 Amazon 适用于 Node.js 的 SDK 来描述如何使用 Amazon SDK 创建 API Gateway API。有关使用 Amazon SDK 的更多信息,包括如何设置开发环境,请参阅 Amazon SDK
使用适用于 Node.js 的 Amazon 软件开发工具包设置 API 需要调用createRestApi
、createResource
或getResources
putMethod
、putMethodResponse
、putIntegration
、和putIntegrationResponse
函数。
以下过程将引导您完成使用这些 SDK 命令设置支持GET /pets
和GET /pets/{petId}
方法的简单 PetStore API 的基本步骤。
使用适用于 Node.js 的 Amazon SDK 设置简单的 PetStore API
-
实例化开发工具包:
var AWS = require('aws-sdk'); AWS.config.region = 'us-west-2'; var apig = new AWS.APIGateway({apiVersion: '2015/07/09'});
-
调用
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。 -
调用
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 资源树的起点,有了它才能执行下一步操作。 -
调用
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
资源。 -
调用
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
值映射并传递到后端。 -
调用
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); } })
-
调用
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" } }
-
调用
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); })
-
最好先测试调用 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); } })
-
最后,您可以部署 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); } })