简体   繁体   中英

how do you 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?

When you are calling 'newClient' method of gateway client factory in your code:

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

It expects a key 'invokeUrl' in the configuration object. So you need to pass this key with your specified URL as value of this key. For example: you should try this -

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

Hope, it may help you to resolve this issue.

On an alternate path, why don't you directly call the API-Gateway's API as a rest call? Simply call the api url and pass key as 'x-api-key' in header. This will keep things simple and east to understand for other devs also.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM