如何从节点正确调用aws api网关

时间:2017-06-03 19:01:13

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

我在以下节点代码中调用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

最合适的方法是什么,以避免出现此错误?

2 个答案:

答案 0 :(得分:2)

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

window.location.reload(true);

我希望它有所帮助。

答案 1 :(得分:0)

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