Hello, i have found and error in _ajax_for_nodejs module in JS SDK.
Error is in Backendless._ajax_for_nodejs, line 388:
on this line you handle data event by sending chunk to success handler,
which causes entire request to fail if data is segmented.
Solution is quite simple:
- on 'data' event fill a buffer string
on 'end' event call config.asyncHandler.success with content of buffer as argument
Backendless._ajax_for_nodejs = function (config) {
config.data = config.data || "";
if (typeof config.data !== "string") {
config.data = JSON.stringify(config.data);
}
config.asyncHandler = config.asyncHandler || {};
config.isAsync = (typeof config.isAsync == 'boolean') ? config.isAsync : false;
var protocol = config.url.substr(0, config.url.indexOf('/', 8)).substr(0, config.url.indexOf(":"));
var uri = config.url.substr(0, config.url.indexOf('/', 8)).substr(config.url.indexOf("/") + 2),
host = uri.substr(0, (uri.indexOf(":") == -1 ? uri.length : uri.indexOf(":"))),
port = uri.indexOf(":") != -1 ? parseInt(uri.substr(uri.indexOf(":") + 1)) : (protocol == "http" ? 80 : 443);
var options = {
host: host,
//protocol: "http",
port: port,
method: config.method || "GET",
path: config.url.substr(config.url.indexOf('/', 8)),
//body: config.data,
headers: {
"Content-Length": config.data ? config.data.length : 0,
"Content-Type": config.data ? 'application/json' : 'application/x-www-form-urlencoded',
"application-id": Backendless.applicationId,
"secret-key": Backendless.secretKey,
"application-type": "JS"
}
};
var buffer = '';
if (currentUser != null) {
options.headers["user-token"] = currentUser["user-token"];
}
if (!config.isAsync) {
var http = require("httpsync"),
req = http.request(options);
} else {
var httpx = require(protocol);
var req = httpx.request(options, function (res) {
res.setEncoding('utf8');
res.on('data', function (chunk) {
buffer += chunk;
});
res.on('end', function()
{
config.asyncHandler.success(buffer);
})
});
}
req.on('error', function (e) {
config.asyncHandler.fault || (config.asyncHandler.fault = function () {
});
config.asyncHandler.fault(e);
});
req.write(config.data, "utf8");
return req.end();
};