Require Middleware
This library will give you the capabilities of middleware in calls to require. It's super handy for unit testing and dependency injection, but could be used for broader application purposes as well.
This is heavily inspired by, and modeled after, Connect middleware.
Installation
Use --save
if using in your core app, --save-dev
if using for testing
npm install [--save/--save-dev] require-middleware
Basic Usage
// Remember this is going to hijack all require calls to follow!// That also means that you might want to load it first var requireMiddleware = ;requireMiddleware; var myModule = ; // Gets resolved as if I called `require('somethingelse');
If your module isn't going to block the request for the middleware (by calling next(err)
) or resolve the dependency by returning the result, calling next()
will execute the next registered middleware.
Throwing errors
Simply pass a return value to next and that will be thrown as an exception
{ if reqname === 'fs' ; }
Fiddling with registered middleware
The middleware stack is exposed on requireMiddleware.stack
.
Naming your middleware
requireMiddleware;// ORrequireMiddleware;
The stack, after registering this middleware would look like:
handle: Function myMiddleware name: 'myMiddleware' handle: Function someOtherMiddleware name: 'someOtherMiddleware'
The .as()
function is handy if you want to load up common middleware from another location but name it something specific that you control for later manipulation on the stack. Naming middleware will not impact its functionality in any way.