|
380 | 380 |
|
381 | 381 | Backendless._ajax_for_nodejs = function(config) { |
382 | 382 | config.data = config.data || ""; |
| 383 | + config.asyncHandler = config.asyncHandler || {}; |
| 384 | + config.isAsync = (typeof config.isAsync == 'boolean') ? config.isAsync : false; |
| 385 | + |
| 386 | + if (!config.isAsync) { |
| 387 | + throw new Error('Use Async type of request using Backendless with NodeJS. Add Backendless.Async(successCallback, errorCallback) as last argument'); |
| 388 | + } |
383 | 389 |
|
384 | 390 | if (typeof config.data !== "string") { |
385 | 391 | config.data = JSON.stringify(config.data); |
386 | 392 | } |
387 | 393 |
|
388 | | - config.asyncHandler = config.asyncHandler || {}; |
389 | | - config.isAsync = (typeof config.isAsync == 'boolean') ? config.isAsync : false; |
390 | | - |
391 | | - var protocol = config.url.substr(0, config.url.indexOf('/', 8)).substr(0, config.url.indexOf(":")); |
392 | | - var https = protocol === 'https'; |
393 | | - |
394 | | - var uri = config.url.substr(0, config.url.indexOf('/', 8)).substr(config.url.indexOf("/") + 2), |
395 | | - host = uri.substr(0, (uri.indexOf(":") == -1 ? uri.length : uri.indexOf(":"))), |
396 | | - port = uri.indexOf(":") != -1 ? parseInt(uri.substr(uri.indexOf(":") + 1)) : (https ? 443 : 80); |
| 394 | + var u = require('url').parse(config.url); |
| 395 | + var https = u.protocol === 'https:'; |
397 | 396 |
|
398 | 397 | var options = { |
399 | | - host : host, |
400 | | - port : port, |
| 398 | + host : u.host, |
| 399 | + port : u.port || https ? 443 : 80, |
401 | 400 | method : config.method || "GET", |
402 | | - path : config.url.substr(config.url.indexOf('/', 8)), |
| 401 | + path : u.path, |
403 | 402 | headers: { |
404 | 403 | "Content-Length" : config.data ? Buffer.byteLength(config.data) : 0, |
405 | 404 | "Content-Type" : config.data ? 'application/json' : 'application/x-www-form-urlencoded', |
|
413 | 412 | options.headers["user-token"] = currentUser["user-token"]; |
414 | 413 | } |
415 | 414 |
|
416 | | - if (!config.isAsync) { |
417 | | - throw new Error('Use Async type of request using Backendless with NodeJS. Add Backendless.Async(successCallback, errorCallback) as last argument'); |
418 | | - } |
419 | | - |
420 | 415 | var buffer; |
421 | 416 | var httpx = require(https ? 'https' : 'http'); |
422 | 417 | var req = httpx.request(options, function(res) { |
|
440 | 435 | }); |
441 | 436 |
|
442 | 437 | req.on('error', function(e) { |
443 | | - config.asyncHandler.fault || (config.asyncHandler.fault = function() {}); |
444 | | - config.asyncHandler.fault(e); |
| 438 | + config.asyncHandler.fault && config.asyncHandler.fault(e); |
445 | 439 | }); |
446 | 440 |
|
447 | 441 | req.write(config.data); |
|
0 commit comments