Skip to content

danhab99/express-circut-breaker

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

12 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Express Circuit Breaker

npm npm

Provides error protection within an express route

Installation

npm i express-circuit-breaker
var breaker = require('express-circuit-breaker')

Usage

express-circuit-breaker produces a middleware that will block requests if an error was thrown from a previous use.

API

For breaker(opts)

opts.catchError(e)

Called when a child node throws an error. Must return trip or reset.

  • return 'trip': Trips the breaker and blocks future requests
  • return 'reset': Resets the breaker to it's untripped state and allows future requests
opts.handleLater = false

Does not block request, instead allows request to be handled by next middleware. Breaker status is communicated in the req.breakerTripped. If true, breaker will not run handleBlockedRequest.

opts.handleBlockedRequest(req, res)

A middleware for handling incoming requests that were blocked by the breaker

Workflow

request(/protected) -> server -> breaker(open) -> endpoint

      |
      V

request(receives error 500) <- server <- breaker(tripped) <- endpoint(throws error)

Then later...

request(/protected) -> server <- breaker(tripped, sends back 500) -- endpoint(never touched)

Example implementation

var app = require('express')()
var breaker = require('express-circuit-breaker')

var CB = breaker({
  catchError: e => 'trip',
  handleBlockedRequest: (req, res) => res.sendStatus(500)
})

app.get('/unprotected', (req, res) => res.sendStatus(200))

app.get('/protected', CB, (req, res) => res.sendStatus(200))

About

Provides error protection within an express route

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published