Unleash awesomeness. Private packages, team management tools, and powerful integrations. Get started with npm Orgs »

anubis

2.0.0 • Public • Published

anubis

Build Status

Make sure your express application responds correctly when outgoing http requests are failing

When your express application is calling failing resources, mount this middleware to ensure it will respond with a proper 502 Bad Gateway response when needed.

Installation

$ npm install anubis --save

Usage

This is a tiny express app that will do nothing but proxy another resource. In case the call fails with a 5xx error, anubis makes sure the correct 502 status code is returned.

var express = require('express');
var rp = require('request-promise');
var anubis = require('anubis');
 
var app = express();
 
app.get('/', function(req, res, next){
    rp('http://my-unreliable-api.com')
        .then(function(payload){
            res.json({ok: true, payload: payload});
        })
        .catch(next);
});
 
// always make sure to mount anubis **after** all routes that call
// external resources
// pass a function that transforms the Error into a status code
app.use(anubis(function(err){
    return err.statusCode;
}));
 
app.use(function(err, req, res, next){ //eslint-disable-line no-unused-vars
    res.status(err.status);
    res.json({ok: false});
});

License

MIT © Frederik Ring

install

npm i anubis

Downloadsweekly downloads

5

version

2.0.0

license

MIT

homepage

github.com

repository

Gitgithub

last publish

collaborators

  • avatar
Report a vulnerability