Its like this. Thank you.
(can view the code here: http://www.codeshare.io/rA1w9)
var testFunc : function(stuff, callback) {
var result = {
error: false,
data: ''
};
var post_body_xml = xmlbuilder.buildObject(post_body);
// An object of options to indicate where to post to
var post_options = {
host: api.endpoint,
path: api.path,
port: '80',
method: 'POST',
headers: {
'Content-Type': 'text/xml',
'Content-Length': post_body_xml.length
}
};
// Set up the request
var post_req = http.request(post_options, function(res) {
res.setEncoding('utf8');
res.on('data', function(chunk) {
result.data += chunk;
});
res.on('error', function(e) {
console.log('problem with request: ' + e.message);
result = {
error: true,
data: e.message
};
});
res.on('end', function() {
xmlparser.addListener('end', function(json_result) {
result.data = json_result;
//console.log(result);
callback(result);
});
xmlparser.parseString(result.data);
});
});
// post the data
post_req.write(post_body_xml);
post_req.end();
};
Sexta-feira, 21 de Novembro de 2014 13:43:31 UTC, ajlopez escreveu:
>
> Can you include the code for
>
> // do http request & ...
>
> ? And what is the code if do http request fails?
>
> Another point: you don't need
>
> return res.json ..;
>
> Change to
>
> res.json...;
>
> Angel "Java" Lopez
> @ajlopez
>
>
> On Fri, Nov 21, 2014 at 6:40 AM, André Simões <[email protected]
> <javascript:>> wrote:
>
>> Hi. I'm trying to create a component in Node.js that consumes two
>> external APIs.
>>
>> The code of my component is something like:
>>
>> var api1 = require('./api1');
>> var api2 = require('./api2');
>> var component = { api1: api1, api2: api2 };
>> module.exports = component;
>> --------------------
>>
>> api1 is something like this:
>>
>> var api1 = { testFunc: function(stuff,callback){ // do http request &
>> callback(result) } }
>> module.exports = api1;
>>
>> --------------------
>>
>> I've one endpoint on nodejs where i'm trying to use this component, like
>> this:
>>
>> var component = require('../../component');
>> component.api1.testFunc(1, function(result) {return res.json(200,
>> result.data);});
>>
>> --------------------
>>
>> It works fine for the first time. On the second call i have no response
>> from server.
>> Newbie here. Can someone help me please?
>>
>> Thanks in advance
>>
>> --
>> Job board: http://jobs.nodejs.org/
>> New group rules:
>> https://gist.github.com/othiym23/9886289#file-moderation-policy-md
>> Old group rules:
>> https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
>> ---
>> You received this message because you are subscribed to the Google Groups
>> "nodejs" group.
>> To unsubscribe from this group and stop receiving emails from it, send an
>> email to [email protected] <javascript:>.
>> To post to this group, send email to [email protected]
>> <javascript:>.
>> To view this discussion on the web visit
>> https://groups.google.com/d/msgid/nodejs/43a91f03-1e4d-4aac-8c3a-55715ffe47d3%40googlegroups.com
>>
>> <https://groups.google.com/d/msgid/nodejs/43a91f03-1e4d-4aac-8c3a-55715ffe47d3%40googlegroups.com?utm_medium=email&utm_source=footer>
>> .
>> For more options, visit https://groups.google.com/d/optout.
>>
>
>
--
Job board: http://jobs.nodejs.org/
New group rules:
https://gist.github.com/othiym23/9886289#file-moderation-policy-md
Old group rules:
https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
---
You received this message because you are subscribed to the Google Groups
"nodejs" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/nodejs/293adbdf-183f-4d00-b24b-5bb3bc5b621a%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.