JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 1217
  • Score
    100M100P100Q105704F
  • License MIT

A node.js module to run connect-like middlewares in sequence

Package Exports

  • connect-sequence

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (connect-sequence) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

connect-sequence

Build Status Coverage Status NPM

NPM

GitHub tag npm

JavaScript Style Guide Semver 2.0 Gitter

A node.js module to run connect-like middlewares in sequence

What is connect-sequence

This module is intended to be used in a connect or express application.

In an express application, you manipulate middlewares. The request walkthru the middlewares you registred in the same order you registred it.

This is super cool but sometimes, we would want to register the middleware sequence dynamically, i.e. at runtime.

connect-sequence aims to make a such thing super-easy!

You'll find connect-sequence on these platforms:

Suscribe to new releases on libraries.io!

Usage

Important Note:

The following example is simple, stupid simple, too simple. I know it.

In the usage exampe the conditions tested before pushing the products middlewares in the middlewares array coud/should simply be tested in each middleware that need to test a conditions on the req object before doing its stuff.

But the key point is to show HOW it could be used, so simplicity is ok. You know the real cases when you need to use the "connect-sequence patern", as I know when I needed it before deciding to write this module.

The truth is in general, we need this patern when the conditions are complexes and for some good reasons, in these complexes case, the conditions to use or not this or that one middleware shouldn't be tested into it; often because it would be too complex or even impossible.

In my cases, I often use this patern when I written some usefull tiny middlewares highly reusable in differents contexts, and these contexts shoud be tested out of these middlewares to keep it clean and real reusable. In particular when I want to reuse a middleware for different API versions of the same resource.

/**
 * Product API
 * @module
 */

var connectSequence = require('connect-sequence')
var productsController = require('./products.controller')

module.exports = productRouter

function productRouter (app) {
  app.route('/api/products/:productId').get(function (req, res, next) {
    var middlewares = []

    // build the middlewares sequence
    if (req.query.filter) {
      middlewares.push(productsController.filter)
    }
    if (req.query.format) {
      middlewares.push(productsController.validateFormat)
      middlewares.push(productsController.beforeFormat)
      middlewares.push(productsController.format)
      middlewares.push(productsController.afterFormat)
    }
    if (req.query.filter && req.query.format) {
      middlewares.push(productsController.prepareResponse)
    }
    middlewares.push(productsController.sendResponse)

    // run each middleware in sequence
    connectSequence.run(req, res, next, middlewares)
  })

  app.param('productId', function (req, res, next, id) {
    // ... yield the product by ID and bind it to the req object
  })
}


Installation

With node package manager (recommanded)

npm install --save connect-sequence

Or manually

Download:

wget https://github.com/sirap-group/connect-sequence/archive/v1.0.0.zip

Extract:

unzip v1.0.0.zip

Move in the node_modules directory:

mv connect-sequence ./node_modules/connect-sequence

Credits

Copyright © 2016 SIRAP Group, All Rights Reserved

License

This project is licensed under the MIT license