Making a node.js component

75 views
Skip to first unread message

André Simões

unread,
Nov 21, 2014, 4:40:52 AM11/21/14
to nod...@googlegroups.com
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

Angel Java Lopez

unread,
Nov 21, 2014, 8:43:31 AM11/21/14
to nod...@googlegroups.com
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


--
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 nodejs+un...@googlegroups.com.
To post to this group, send email to nod...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/nodejs/43a91f03-1e4d-4aac-8c3a-55715ffe47d3%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

André Simões

unread,
Dec 1, 2014, 11:03:47 AM12/1/14
to nod...@googlegroups.com
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();

};
Reply all
Reply to author
Forward
0 new messages