connect-sequence

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


Keywords
node, node.js, connect, middleware, express, express-middleware, expressjs, node-module, nodejs, sirap
License
MIT
Install
npm install connect-sequence@1.1.0

Documentation

connect-sequence GitHub license GitHub tag

NPM

node: LTS/argon node: LTS/argon npm

Build Status Coverage Status NPM

Code Climate Issue Count

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!

Installation

With node package manager (recommanded but not required)

npm install --save connect-sequence

API reference

ConnectSequence API reference

Usage

The following example is stupid simple.

In the usage example 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.

You know the real cases when you need to use the "connect-sequence patern". In this example I just show how to use it.

I often use this patern when I write some usefull tiny middlewares highly reusable in differents contexts, and these contexts shoud be tested out of these middlewares to keep it clean and really reusable.

/**
 * 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) {
    // Create a ConnectSequence instance and setup it with the current `req`,
    // `res` objects and the `next` callback
    var seq = new ConnectSequence(req, res, next)

    // build the desired middlewares sequence thanks to:
    // - ConnectSequence#append(mid0, ..., mid1),
    // - ConnectSequence#appendList([mid0, ..., mid1])
    // - and ConnectSequence#appendIf(condition, mid)

    if (req.query.filter) {
      seq.append(productsController.filter)
    }

    if (req.query.format) {
      seq.append(
        productsController.validateFormat,
        productsController.beforeFormat,
        productsController.format,
        productsController.afterFormat
      )
    }

    // unless #run(), the other methods are chainable:

    // append the productsController.prepareResponse middleware to the sequence
    // only if the condition `req.query.format && req.formatedProduct` is true
    // at the moment where the middleware would be called.
    // So the condition is tested after the previous middleware is called and thus
    // if the previous modifies the `req` object, we can test it.
    seq.appendIf(isProductFormatted, productsController.prepareResponse)
    .append(productsController.sendResponse)
    // run the sequence
    .run()
  })

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

  function isProductFormatted (req) {
    return Boolean(req.formatedProduct)
  }
}

Contribute

JavaScript Style Guide

The javascript source files are located in the lib folder and the unit test files are located in the tests folder.

We use the Standard Javascript Code Style to keep the code clean and nice.

We use Gulp some gulp plugins and some other node modules as devDependendies to automate the developement tasks:

Finally, we use the Semver 2.0 (Semantic Versioning) to standardize the release version numbers (major/minor/path/pre-release).

Your contributions posting issues and pull requests are welcome!

Development

Ensure you are not in production environement to install the devDependencies

$ NODE_ENV=development npm install

Then you can start coding in a Test Driven Development environement with gulp, mocha and chai

$ npm run TDD

The script will lint the lib and test files (but not break on error), run all the unit tests and then it will restart the tests on file change.

Credits

Copyright

Copyright © 2016 SIRAP Group, All Rights Reserved

License

This project is licensed under the MIT license