express-context
Middleware for express to seamlessly contextualize specific properties.
It allows for other middleware to be oblivious that properties they are manipulating are encapsulated within a specific context. For example, a property "foo" contextualized on a per-middleware basis means that every time middleware tries to access req.foo
it manipulates a variable specific to that piece of middleware. You may then later recover all the different versions of foo
, each specific to one particular middleware.
var express = contextualize = app = ; { reqfoo = 5; reqbar = 10; ;} { reqfoo = 7; reqbar = 1; ;} var context = ; var cmw1 = context cmw2 = context; app;
Building Fluent APIs
Create your API by creating a new context with an ID that's unique to your module, declaring all the properties that your API is concerned with, and then adding in all the mixins your module provides:
var context = ; var mixins = { return this; } { return ;} moduleexports = api;
Then make use of your API:
var api = app = ; app;app;
API
use(method1, method2, ...)
Continue the chain with a new method. This works just like how it does in express
.
// res is now middleware that invokes context AND the new given functionvar res = context;
mixin(properties)
Add new properties to the chain. This does not mutate the chain on which it was called.
// res now has everything in context plus foo = 5var res = context;
for(middleware)
Get chained middleware representing the contextualized version of the arguments. This does not do anything but set the current context.
// res is a chain whose context is 'abc'var res = context;
// res is a function for the parallel execution of contextualized versions of// anything used after it in the chain.var res = context;
of(request)
Get the value of the chain's context for a specific request
// res is an object whose keys correspond to all the contexts that were set// during the processing of req.var res = context;
// res is the result of the single context for mw1 that was set during the// processing of req.var res = context;