npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

connect-composer

v1.0.0

Published

Connect and reuse connect middlewares

Downloads

2

Readme

connect-composer

Connect and reuse connect/ express middlewares

NPM version Build Status

Compose connect/ express compatible middlewares and reuse or extend them.

Features:

  • Stack middlewares
  • Use connect middlewares without or with connect, express
  • Trap errors within middlewares using function (err, req, res, next) functions
  • Safely catch errors within middlewares
  • Modify and reuse existing middlewares

Table of Contents

Description

Stack Middlewares

This module allows to join middlewares together:

var compose = require('connect-composer')
var req = { test: [] }
var res = {}
var middlewares1 = [
  function (req, res, next) { req.test.push('one'); next() },
  function (req, res, next) { req.test.push('two'); next() }
]
var middlewares2 = [
  function (req, res, next) { req.test.push('three'); next() },
  function (req, res, next) { req.test.push('four'); next() }
]
// create a new middleware
var newMiddlewares = compose(middlewares1, middlewares2)
// run new composed middleware
newMiddlewares(req, res, function () {
  console.log(req.test) // < [ 'one', 'two', 'three', 'four' ]
})

Stack composed middlewares

You can also stack composed middlewares:

var compose = require('connect-composer')
var req = { test: [] }
var res = {}
// pass as Array
var middlewares1 = compose([
  function (req, res, next) { req.test.push('one'); next() },
  function (req, res, next) { req.test.push('two'); next() }
])
// or by Argument
var newMiddlewares = compose(
  middlewares1,
  function (req, res, next) { req.test.push('three'); next() },
  function (req, res, next) { req.test.push('four'); next() }
)
// run new composed middleware
newMiddlewares(req, res, function () {
  console.log(req.test) // < [ 'one', 'two', 'three', 'four' ]
})

Trap and catch errors

Traps errors and catches errors within middlewares (prevents server from crashing)

var compose = require('connect-composer')
var req = { test: [] }
var res = {}
var middlewares = compose(
  function (req, res, next) { req.test.push('one'); next() },
  function (req, res, next) {
    next('badly')           // middleware calls `next` with error parameter
  },
  function (req, res, next) {
    req.test.push('two')    // is never called
    next()
  },
  function (err, req, res, next) { // error is trapped here; function has arity 4
    console.log(err + ' trapped')  // < badly trapped
    next()                  // continue with the processing
  },
  function (req, res, next) { req.test.push('three'); next() },
  function (req, res, next) {
    if (1) throw new Error('another error') // middleware calls `next` with error parameter
    next()
  },
  function (req, res, next) {
    req.test.push('four')   // is never called
    next()
  }
)
// run new composed middleware
middlewares(req, res, function (err) {
  console.log(err)      // < [Error: another error] is catched
  console.log(req.test) // < [ 'one', 'three' ]
})

Manipulate and reuse middlewares

Use the following methods to change an existing composed middleware

  • unshift(middlewares) prepend middlewares to the front of the stack
  • push(middlewares) push middlewares to the end of the stack
  • before(selector, middlewares) insert middlewares before selector
  • after(selector, middlewares) insert middlewares after selector
  • replace(selector, middlewares) replace middlewares with name selector with middlewares
  • remove(selector) remove middlewares with name selector
  • clone() clone composed middlewares
var compose = require('connect-composer')
var res = {}
var initial = {
  two: function (req, res, next) { req.test.push('two'); next() },
  four: function (req, res, next) { req.test.push('four'); next() }
}
var others = {
  one: function one (req, res, next) { req.test.push('one'); next() },
  three: function three (req, res, next) { req.test.push('three'); next() },
  five: function othersFive (req, res, next) { req.test.push('five'); next() },
  six: function six (req, res, next) { req.test.push('six'); next() },
  seven: { seven: function (req, res, next) { req.test.push('seven'); next() } },
  eight: function (req, res, next) { req.test.push('eight'); next() }
}
// create a composed middleware
var composed = compose(initial)

// do some manipulation
composed.unshift(others.one)              // prepend
composed.push(others.five)                // append
composed.before('four', others.three)     // insert before
composed.after('othersFive', others.six)  // insert after
composed.after('six', others.seven)

// named functions become named middleware functions
console.log(composed.stack) // [ { one: [Function: one] },
                            //   { two: [Function] },
                            //   { three: [Function: three] },
                            //   { four: [Function] },
                            //   { othersFive: [Function: othersFive] },
                            //   { six: [Function: six] },
                            //   { seven: [Function] } ]

// lets clone the middlewares
var composed2 = composed.clone() // clone the middlewares; same as `compose(composed)`
composed2.remove('six').remove('two').remove('four') // remove middlewares

// do some more manipulation
composed.replace('seven', others.eight)   // replace middleware seven with eight

// run new composed middleware
var req = { test: [] }
composed(req, res, function () {
  console.log(4, req.test) // < [ 'one', 'two', 'three', 'four', 'five', 'six', 'eight' ]
})

// run the other composed middleware (with a different request)
var req2 = { test: [] }
composed2(req2, res, function () {
  console.log(5, req2.test) // < [ 'one', 'three', 'five', 'seven' ]
})

Example

Run the examples above with node test/sample.js.

Methods

Returns: function - middleware function

| Param | Type | | --- | --- | | | function | Array | Object |

Kind: static property of compose Returns: function - middleware

| Param | Type | Description | | --- | --- | --- | | selector | String | selector for named middleware | | middlewares | Array | Object | |

Kind: static property of compose Returns: function - middleware

| Param | Type | Description | | --- | --- | --- | | selector | String | selector for named middleware | | middlewares | Array | Object | |

Kind: static property of compose Returns: function - middleware

| Param | Type | Description | | --- | --- | --- | | selector | String | selector for named middleware | | middlewares | Array | Object | |

Kind: static property of compose Returns: function - middleware

| Param | Type | Description | | --- | --- | --- | | selector | String | selector for named middleware |

Kind: static property of compose Returns: function - middleware

| Param | Type | | --- | --- | | middlewares | Array | Object |

Kind: static property of compose Returns: function - middleware

| Param | Type | | --- | --- | | middlewares | Array | Object |

Kind: static method of compose Returns: Array - - array of middlewares {Object|Array}

| Param | Type | | --- | --- | | middlewares | Object | Array | function |

Kind: static method of compose Returns: function - cloned middleware function

compose.noop()

No operation middleware - just calls next

Kind: static method of compose

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.

References