5
I work with an Amazon JSON API, in which I research products and handle information according to their results, but within Nodejs the API information is written on the console, but it is not written in the call response. How to respond only after data returned from Amazon?
Follow the example of the code:
var http = require("http");
//Biblioteca para conexão e pesquisa nos servidores da Amazon
var aws = require("aws-lib");
http.createServer(function (req, res) {
res.writeHead(200, {"Content-Type": "application/json"});
var prodAdv = aws.createProdAdvClient(yourAccessKeyId, yourSecretAccessKey, yourAssociateTag);
var options = {SearchIndex: "Books", Keywords: "Javascript"};
var resposta = "";
prodAdv.call("ItemSearch", options, function(err, result) {
console.log(result);
resposta = result;
});
res.end(resposta);
}).listen(1337, "127.0.0.1");
PS: The function parameters createProdAdvClient
have been amended for security reasons, for all purposes are completed.
This problem of understanding the asynchronous paradigm is so common that if you search for
Synchronize asynchronous calls in node
in Google you will find several implementations of modules that address this problem.– João Paraná