Saturday, 3 June 2017

how to properly invoke aws api gateway from node

I'm invoking aws api gateway in the following node code:

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.
        });
    });

};

I get the following error:

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

What the the most proper way to do this, to avoid getting this error?



via Eugene Goldberg

No comments:

Post a Comment