Skip to content
This repository was archived by the owner on Feb 28, 2020. It is now read-only.

m90/anubis

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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

About

Make your express application respond correctly when http requests are failing

Resources

Stars

Watchers

Forks

Packages

 
 
 

Contributors