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
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...;
Its like this. Thank you. 
(can view the code here: http://www.codeshare.io/
var testFunc : function(stuff, callback) {
    var result = {
        error: false,
        data: ''
    };
    var post_body_xml = xmlbuilder.buildObject(post_
    // 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.
        });
    });
    // post the data
    post_req.write(post_body_xml);
    post_req.end();
};
 
댓글 없음:
댓글 쓰기