我在以下节点代码中调用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
为了避免出现此错误,最合适的方法是什么?
最佳答案
您传递给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.
});
});
};
希望对您有所帮助。
关于node.js - 如何从 Node 正确调用AWS API网关,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/44347567/