node.js - 如何从 Node 正确调用AWS Api Gateway?

标签 node.js amazon-web-services aws-api-gateway

我正在尝试使用 aws-api-gateway-client 模块从 NodeJS 调用 AWS api 网关。

这是我的代码:

module.exports = function(app) {

    var apigClientFactory = require('aws-api-gateway-client').default;

    var querystring = require('querystring');

    var apigClient = apigClientFactory.newClient({
        apiKey: '1234'
    });

    var params = {
        //This is where any header, path, or querystring request params go. The key is the parameter named as defined in the API
        userId: '1234'
    };
    var additionalParams = {};

    app.get('*', function(req, res) {
        res.sendfile('./public/index.html');
    });

    app.post("/customerinfo", function(req, res) {
        console.log("name: " + req.body["customer_name"]);

        var body = {"async": true,
            "crossDomain": true,
            "url": "https://mystuff.execute-api.us-west-2.amazonaws.com/staging/api",
            "method": "POST",
            "headers": {
            "cache-control": "no-cache"
        },
        "data": querystring.stringify(req.body["customer_name"])
    };

        apigClient.invokeApi(params, body, additionalParams)
            .then(function(result){
                // Add success callback code here.
            }).catch( function(result){
            // Add error callback code here.
        });
    });

};

一旦启动 Node 服务器,我就会收到此错误:

/Users/eugene/Desktop/dms/node_modules/aws-api-gateway-client/dist/apigClient.js:84
  var endpoint = /(^https?:\/\/[^\/]+)/g.exec(invokeUrl)[1];
                                                        ^

TypeError: Cannot read property '1' of null
    at Object.apigClientFactory.newClient (/Users/eugene/Desktop/dms/node_modules/aws-api-gateway-client/dist/apigClient.js:84:57)
    at module.exports (/Users/eugene/Desktop/dms/app/routes.js:7:40)
    at Object.<anonymous> (/Users/eugene/Desktop/dms/server.js:26:24)
    at Module._compile (module.js:409:26)
    at Object.Module._extensions..js (module.js:416:10)
    at Module.load (module.js:343:32)
    at Function.Module._load (module.js:300:12)
    at Module.runMain [as _onTimeout] (module.js:441:10)
    at Timer.listOnTimeout (timers.js:92:15)

从 Node 调用 AWS Api Gateway 的正确方法是什么?

最佳答案

当您在代码中调用网关客户端工厂的“newClient”方法时:

var apigClient = apigClientFactory.newClient({
    apiKey: '1234'
});

它需要配置对象中的关键“invokeUrl”。因此,您需要将此 key 与您指定的 URL 作为此 key 的值一起传递。例如:你应该尝试这个 -

var apigClient = apigClientFactory.newClient({
    apiKey: '1234',
    invokeUrl:'https://xxxxx.execute-api.us-east-1.amazonaws.com'
});

希望它可以帮助您解决这个问题。

关于node.js - 如何从 Node 正确调用AWS Api Gateway?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44346724/

相关文章:

amazon-web-services - AWS API Gateway (REST) - 即使存在未知属性,请求验证也会通过

javascript - 为什么我的 RegExp 无法搜索 IP 服务器

amazon-web-services - 如何从 Lambda 函数与私有(private) EC2 实例通信

amazon-web-services - ECS 是否使用 Docker 层缓存来加速部署?

amazon-web-services - 从 cloudformation 在 API Gateway 中获得意外结果

amazon-web-services - AWS CloudFormation ApiGatewayV2 需要目标属性

node.js - Sequelize : Don't return password on Create

node.js - 部署到 AWS Elastic Beanstalk 失败,出现异常 "invalid byte sequence in UTF-8"

node.js - 如何在浏览器中使用 typescript ?

amazon-web-services - 使用 cloudformation 模板创建 ec2 实例的镜像