-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
76 lines (56 loc) · 1.81 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
"use strict";
var request = require('request');
module.exports = require('./javascriptSDK/noserv.js');
module.exports.isRunning = {};
module.exports.reqQueue = {};
module.exports.Noserv.sendAjax = function(method, url, data, callF, addF){
var jsonData = JSON.parse(data);
var appId = jsonData._ApplicationId;
if(appId && module.exports.isRunning[appId]) {
if(!module.exports.reqQueue[appId])
module.exports.reqQueue[appId] = [];
module.exports.reqQueue[appId].push({
method : method,
url : url,
data : data,
callF : callF,
addF : addF
});
return;
}
if(appId)
module.exports.isRunning[appId] = true;
var options = {
method : method,
uri : url,
timeout : 10000,
body : data,
headers : {
'content-type' : 'text/plain'
}
};
request(options, function(err, res) {
if(appId)
module.exports.isRunning[appId] = false;
if(err) {
if (callF.error)
callF.error(data, err.message);
} else {
var response = null;
if(res.body && res.body.charAt && res.body.charAt(0) === '{')
response = JSON.parse(res.body);
else
response = res.body;
if(addF)
addF(response);
if(callF && callF.success)
callF.success(response);
}
if(module.exports.reqQueue[appId] && module.exports.reqQueue[appId].length > 0) {
var arg = module.exports.reqQueue[appId].shift();
process.nextTick(function() {
module.exports.Noserv.sendAjax(arg.method, arg.url, arg.data, arg.callF, arg.addF);
});
}
});
};