I wrote some universal JavaScript method for my apps, across Node.JS and Titanium Mobile, Freeswitch, jQuery. Here introduces a very simple but useful method, http request.
We all doing HTTP request on our apps. Make a request via HTTP and get back the response. I designed an API that I can use the same method signature when I am developing apps for Node.JS and Titanium Mobile. Node.JS is a server side program, and Titanium Mobile is a framework compile to Native iOS code. Freeswitch is an open source SIP Media Server, supports JavaScripts to create IVR apps. jQuery is a web based client framework. These platforms had different underlying native libraries to do HTTP request. I just want to design a very simple API that can wrap up these platforms, then I can share code easily across these platform.
The client code I can do:
frequest({
url : 'http://somewebsite.com',
callback : function(data) {
console.log(data);
}
});
The code for Node.JS:
frequest : function(args) {
var http = require('http');
try {
var options = {};
if(args.options) {
options = args.options;
}
if(args.url) {
if(args.url.indexOf('http') == -1) return;
var b = require('url').parse(args.url);
// resolve host name
if(b.hostname) {
options.host = b.hostname;
}
// resolve port
if(!b.port) {
b.port = 80;
}
if(b.port) {
options.port = b.port;
}
// resolve web path
if(b.pathname) {
options.path = b.pathname;
if(b.search) {
options.path += b.search;
}
}
}
if(args.headers) {
options.headers = args.headers;
}
var request = http.get(options);
if(args.callback || args.callbackJSON) {
request.addListener('response', function(response){
var data = '';
response.addListener('data', function(chunk){
data += chunk;
});
response.addListener('end', function(){
// prepare data for callback
if(data != '') {
if(args.callback) {
args.callback(data);
}
if(args.callbackJSON) {
try {
var json = JSON.parse(data);
args.callbackJSON(json);
} catch (e) {
console.log(e);
}
}
}
});
});
}
} catch (e) {
console.log(e);
}
}
The code for Titanium Mobile:
var frequest = function(args) {
var xhr = Ti.Network.createHTTPClient();
xhr.onload = function() {
var res = this.responseText;
if(args.callback) {
args.callback(res);
}
if(args.callbackJSON) {
args.callbackJSON(JSON.parse(res));
}
};
xhr.onerror = function(e) {
// detect message
var errortitle = 'Connection Failure Error';
var errormsg = ''; // define some suggested network failure message
if(e.error && e.error.indexOf('A connection failure occurred')) {
errormsg = 'A connection failure occurred';
}
if(args.errorCallback) {
args.errorCallback({
e : e,
errormsg : errormsg
});
} else {
Ti.UI.createAlertDialog({
title : errortitle,
message : errormsg
}).show();
}
}
if(args.timeout) {
xhr.timeout = args.timeout;
}
if(args.progressCallback) {
xhr.onsendstream = function(e) {
args.progressCallback(e.progress);
}
}
var method = args.method || 'GET';
xhr.open(method, args.url);
if(args.headers) {
for(var k in args.headers) {
var v = args.headers[k];
xhr.setRequestHeader(k,v);
}
}
var params = args.params || null;
if(params != null)
xhr.send(params);
else
xhr.send();
};
The code for Freeswitch:
frequest : function(args) {
var result = fetchUrl(args.url);
if(args.callback) {
args.callback(result);
}
}
The code for jQuery:
var frequest = function(args) {
$.ajax({
url : args.url,
success : args.callback
});
};
Feel free to share your code for related frequest implementation.