middleware-condition

npm install middleware-condition
1 downloads in the last month

middleware-condition

if-else statement as a middleware


condition = require 'middleware-condition'

# the condition is dependant on the state
# in this example we use this simplified conditions
alwaysTrue = (req) -> true
alwaysFalse = (req) -> false

# this would be a useful condition
isLoggedIn = (req) -> req.session?.user?

log = (msg) -> (req, res, next) -> next console.log msg


http.createServer sequenz [
    condition alwaysTrue log('this middleware is executed'), log('this middleware is ignored')
    condition alwaysFalse log('this middleware is ignored'), log('this middleware is executed')
    condition alwaysFalse log('nothing is shown and the next middleware is executed')
    (req, res, next) -> req.end()
npm loves you