• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

express/node route question.

sourceninja

Diamond Member
I'm new to working with node/express and I'm playing with creating an application.

What I have is a 3rd party server that supplies xml data with xml requests (made as post over https). I have all this working now, the user goes to a page, requests data and the page makes a ajax call back to my server with a url like https://myserver.com/api/getData and the post data of the the parameters to plug into the xml.

My node app then takes those parameters and plugs them into some preformed xml (after validating them) and fires it off to the 3rd party server which returns result xml which is formatted and displayed via a view.

What I'd like to do is take the code that makes this request and stick it in a single function that I can call from all my routes. In this way if the backend ever changes how it wants the XML I can just change that function in once place.

So basically the function is something like.
Code:
function getMeSomeXML(xmlData)
    var options = {
    hostname: 'back.end.host.com', port: 443,
    path: '/some/path/they/require',
    method: 'POST',
    headers: {
        'Content-Type': 'application/x-www-form-urlencoded', 
        'Content-Length': xmlData.length
    }
    var req = https.request(options, function(rs)
    {
        rs.setEncoding('utf-8');
        var responseString = '';
        rs.on('data', function(data) {
           responseString += data;
        });

        rs.on('end', function() {
           res.write(responseString);
           res.end();
           return responseString;
        });
    });
    req.write(sendData);
    req.on('error', function(err) {
      // Deal with error
    });
    req.end();    
  };
};

The problem is I can't figure out how to include it in my multiple routes and I don't really want to make it a global function. Any insight?
 
I'm new to working with node/express and I'm playing with creating an application.

What I have is a 3rd party server that supplies xml data with xml requests (made as post over https). I have all this working now, the user goes to a page, requests data and the page makes a ajax call back to my server with a url like https://myserver.com/api/getData and the post data of the the parameters to plug into the xml.

My node app then takes those parameters and plugs them into some preformed xml (after validating them) and fires it off to the 3rd party server which returns result xml which is formatted and displayed via a view.

What I'd like to do is take the code that makes this request and stick it in a single function that I can call from all my routes. In this way if the backend ever changes how it wants the XML I can just change that function in once place.

So basically the function is something like.
Code:
function getMeSomeXML(xmlData)
    var options = {
    hostname: 'back.end.host.com', port: 443,
    path: '/some/path/they/require',
    method: 'POST',
    headers: {
        'Content-Type': 'application/x-www-form-urlencoded', 
        'Content-Length': xmlData.length
    }
    var req = https.request(options, function(rs)
    {
        rs.setEncoding('utf-8');
        var responseString = '';
        rs.on('data', function(data) {
           responseString += data;
        });

        rs.on('end', function() {
           res.write(responseString);
           res.end();
           return responseString;
        });
    });
    req.write(sendData);
    req.on('error', function(err) {
      // Deal with error
    });
    req.end();    
  };
};

The problem is I can't figure out how to include it in my multiple routes and I don't really want to make it a global function. Any insight?

create a file i.e. myFile.js

inside the file write your requirejs code. export the functions that you want to be public.

i.e. module.exports.myFunction = function(myParameter) { }

use it in other files:

var myFile = require('path_to_my_file/myFile');
var result = myFile.myFunction(myParameter);

http://nodejs.org/api/modules.html
 
Last edited:
Back
Top