A node.js module to run connect-like middlewares in 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:
- Github.io: https://sirap-group.github.io/connect-sequence
- github.com: https://github.com/sirap-group/connect-sequence
- npmjs.com: https://www.npmjs.com/package/connect-sequence
- Libraries.io: https://libraries.io/npm/connect-sequence
- Travic CI: https://travis-ci.org/sirap-group/connect-sequence
- Coveralls.io: https://coveralls.io/github/sirap-group/connect-sequence
With node package manager (recommanded but not required)
npm install --save connect-sequence
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)
}
}
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:
- gulp-standard-bundle to lint all the javascript source files against the javascript syntax and the StandardJS code style,
- grunt-mocha and chaijs to make and run the unit tests,
- gulp-coverage and gulp-coveralls to compute and publish the testing coverage of the code,
- gulp-bump and gulp-git to tag the patch, minor and major releases.
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!
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.
- Rémi Becheras (https://github.com/rbecheras)
- Groupe SIRAP (https://github.com/sirap-group)
Copyright © 2016 SIRAP Group, All Rights Reserved
This project is licensed under the MIT license