Saturday 3 June 2017

how to properly call AWS Api Gateway from node?

I'm trying to call AWS api gateway from NodeJS, using aws-api-gateway-client module.

Here is my code:

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

};

As soon as I start the node server, I get this error:

/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)

What is the proper way to call AWS Api Gateway from Node?



via Eugene Goldberg

No comments:

Post a Comment