0.1.0 • Published 10 years ago

connect-condition v0.1.0

Weekly downloads
-
License
MIT
Repository
-
Last release
10 years ago

connect-condition

middleware to conditionally select middlewares

NPM version Build Status

Table of Contents

Description

Use conditional middlewares within connect or express.

If condition returns true, then middleware_true is processed. Otherwise it's middleware_false.

Returns: function - function (req, res, next)

ParamTypeDescription
conditionfunctionfunction (req, res)
middleware_truefunctionfunction (req, res, next) middleware executed if condition returns true
middleware_falsefunctionfunction (req, res, next) middleware executed if condition returns false

Example

require('http').createServer(
  condition(
    function (req, res) { // condition
      if (~req.url.indexOf('json')) {
        return true
      }
    },
    function (req, res, next) { // middleware which is choosen on condition == true
      res.setHeader('Content', 'application/json')
      res.end('{ "text": "as json" }\n')
    },
    function (req, res, next) { // middleware which is choosen on condition == false
      res.end('just text\n')
    }
  )
).listen(3000)

Contribution and License Agreement

If you contribute code to this project, you are implicitly allowing your code to be distributed under the MIT license. You are also implicitly verifying that all code is your original work or correctly attributed with the source of its origin and licence.

License

Copyright (c) 2015 commenthol (MIT License)

See LICENSE for more info.

0.1.0

10 years ago