【问题标题】:how to properly invoke aws api gateway from node如何从节点正确调用 aws api 网关
【发布时间】:2017-11-04 23:10:00
【问题描述】:

我在以下节点代码中调用 aws api 网关:

module.exports = function(app) {

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

    var querystring = require('querystring');



    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://myurl.execute-api.us-west-2.amazonaws.com/staging/api",
            "method": "POST",
            "headers": {
            "cache-control": "no-cache"
        },
        "data": querystring.stringify(req.body["customer_name"])
    };
        var apigClient = apigClientFactory.newClient({
            apiKey: '1234',
            invokeUrl:'https://myurl.execute-api.us-west-2.amazonaws.com/staging/api'
        });

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

};

我收到以下错误:

TypeError: method.toUpperCase is not a function
    at Object.apigClientFactory.newClient.apigClient.invokeApi (/Users/eugene/Desktop/dms/node_modules/aws-api-gateway-client/dist/apigClient.js:117:20)
    at Object.handle (/Users/eugene/Desktop/dms/app/routes.js:36:20)
    at next_layer (/Users/eugene/Desktop/dms/node_modules/express/lib/router/route.js:113:13)
    at Route.dispatch (/Users/eugene/Desktop/dms/node_modules/express/lib/router/route.js:117:5)
    at /Users/eugene/Desktop/dms/node_modules/express/lib/router/index.js:222:24
    at Function.proto.process_params (/Users/eugene/Desktop/dms/node_modules/express/lib/router/index.js:288:12)
    at next (/Users/eugene/Desktop/dms/node_modules/express/lib/router/index.js:216:19)
    at next (/Users/eugene/Desktop/dms/node_modules/express/lib/router/index.js:202:18)
    at Layer.staticMiddleware [as handle] (/Users/eugene/Desktop/dms/node_modules/serve-static/index.js:51:61)
    at trim_prefix (/Users/eugene/Desktop/dms/node_modules/express/lib/router/index.js:263:17)
    at /Users/eugene/Desktop/dms/node_modules/express/lib/router/index.js:225:9
    at Function.proto.process_params (/Users/eugene/Desktop/dms/node_modules/express/lib/router/index.js:288:12)
    at next (/Users/eugene/Desktop/dms/node_modules/express/lib/router/index.js:216:19)
    at Layer.methodOverride [as handle] (/Users/eugene/Desktop/dms/node_modules/method-override/index.js:75:5)
    at trim_prefix (/Users/eugene/Desktop/dms/node_modules/express/lib/router/index.js:263:17)
    at /Users/eugene/Desktop/dms/node_modules/express/lib/router/index.js:225:9

避免出现此错误的最正确方法是什么?

【问题讨论】:

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


【解决方案1】:

您传递给 invokeApi 方法的参数不是客户端中预期的方法签名。应该是这样的,

module.exports = function(app) {

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

    var querystring = require('querystring');



    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://myurl.execute-api.us-west-2.amazonaws.com/staging/api",
            "method": "POST",
            "headers": {
                "cache-control": "no-cache"
            },
            "data": querystring.stringify(req.body["customer_name"])
        };
        var apigClient = apigClientFactory.newClient({
            apiKey: '1234',
            invokeUrl: 'https://myurl.execute-api.us-west-2.amazonaws.com/staging/api'
        });

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

};

希望对你有帮助。

【讨论】:

  • 您好,感谢您分享您的实现,非常感谢!我可以问一个问题吗?请在此处查看更多详细信息github.com/kndt84/aws-api-gateway-client/issues/58 基本上我是 AWS 的新手,并且已经研究过 AWS MobileHub,但我宁愿独立实施这些服务。我目前正在使用aws-api-gateway-client,它效果很好,但我对将 react 本机应用程序和 react js 仪表板与 API Gateway 接口的方法有点困惑。此外,您如何为整个 React Native 应用程序初始化 aws-api-gateway-client
【解决方案2】:

您可能需要仔细检查调用 API 的参数。它应该是: apigClient.invokeApi(params, pathTemplate, method, additionalParams, body)

【讨论】:

    猜你喜欢
    • 2017-11-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-04
    • 1970-01-01
    • 2021-01-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多