Skip to content

External Hosts

Requests to external hosts must be sent asynchronously - the node environment does not allow blocking network IO calls. The following example demonstrates the approach:

Backendless.ServerCode.Persistence.beforeCreate('*', function(req) {
  const http = require('http');

  return new Promise((resolve, reject) => {
    http.get('http://some.external.host.com/', (res) => {
      let body = '';

      res.on('data', (chunk) => body += chunk);
      res.on('end', () => {
        req.item.apiCallResult = body;

        resolve();
      });

      res.resume();
    }).on('error', reject);
  });
});