Skip to content

Simple middleware pattern implementation on top of async.series

License

Notifications You must be signed in to change notification settings

elmigranto/jsmw

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

jsmw

npm install jsmw for a simple middleware pattern implementation on top of async.series.

// Require a function.
const jsmw = require('jsmw');

// Create the chain of middlewares.
const chain = jsmw()
  .use((ctx, next) => {
    ctx.beforeTick = Date.now();
    next();
  })
  .use((ctx, next) => {
    process.nextTick(() => {
      ctx.afterTick = Date.now();
      next();
    });
  })
  .use((ctx, next) => {
    ctx.diff = ctx.afterTick - ctx.beforeTick;
    next();
  });

// Execute your chain with different contexts.
const context = {};
chain.execute(context, err => {
  return err
    ? console.error(err)
    : console.log(JSON.stringify(context, null, 2));
});

Tests have more examples.

Compatibility

Works with node >=4 by default, but should run everywhere async works, with transpilation being required for some JS environments.

About

Simple middleware pattern implementation on top of async.series

Resources

License

Stars

Watchers

Forks

Packages

No packages published