superagent-failover

superagent failover plugin

Usage no npm install needed!

<script type="module">
  import superagentFailover from 'https://cdn.skypack.dev/superagent-failover';
</script>

README

superagent-failover

Appends a .failover method to make trying other URLs when the main URL failed. The list of URLs will be provided through parameters of method .failover together with the timeout that the superagent request should wait to retry.

Install

$ npm install --save superagent-failover

Usage

failover() function call with one host address (IP or domain name with port) as parameter:

var superagent = require('superagent');
require('superagent-failover')(superagent);

superagent
  .get('https://down.yourdomain.com/path/to/page')
  .accept('application/json')
  .failover('backup.yourdomain.com')
  .end(function (err, res) {
    console.log('Response status: ', res.status)
    console.log('... and body: ', JSON.stringify(res.body, null, 2));
  });

failover() function call with one host address and timeout as parameters:

var superagent = require('superagent');
require('superagent-failover')(superagent);

superagent
  .get('https://down.yourdomain.com/path/to/page')
  .accept('application/json')
  .failover({
    url: 'backup.yourdomain.com',
    timeout: 3000
  })
  .end(function (err, res) {
    console.log('Response status: ', res.status)
    console.log('... and body: ', JSON.stringify(res.body, null, 2));
  });

failover() function call with a list of host addresses as parameter:

var superagent = require('superagent');
require('superagent-failover')(superagent);

superagent
  .get('https://down.yourdomain.com/path/to/page')
  .accept('application/json')
  .failover([
    'backup.yourdomain.com',
    'bak.yourdomain.com:81',
    'bak.yourdomain.com:82',
  ])
  .end(function (err, res) {
    console.log('Response status: ', res.status)
    console.log('... and body: ', JSON.stringify(res.body, null, 2));
  });

failover() function call with a list of host addresses as parameter, some urls can have its own timeout (default timeout is 2000ms):

var superagent = require('superagent');
require('superagent-failover')(superagent);

superagent
  .get('https://down.yourdomain.com/path/to/page')
  .accept('application/json')
  .failover([
    {
      url: 'backup.yourdomain.com',
      timeout: 3000
    },
    'bak.yourdomain.com:81',
    'bak.yourdomain.com:82',
  ])
  .end(function (err, res) {
    console.log('Response status: ', res.status)
    console.log('... and body: ', JSON.stringify(res.body, null, 2));
  });