Require-fu is a library designed to provide dependency injection style requires recursively for directories.
Require-fu is based on the idea of dependency injection requiring. Take an express application as an example. One way you might use an express app is to define an index.js in your routes directory that returns a hash of things to wire into Express. For example:
var routes = ;app;app;
This is less than ideal because information about how to access the code that processes the route is separate from the route itself. I, instead, prefer an injection approach. Like so:
app parameter is passed in, and the route's index.js file is responsible for wiring everyone
up. Or, more likely,
routes/index.js just requires a lot of other files that would do that. So,
I may define a
routes/index.js like so:
Then, my actual route files will look like:
I like this style a lot, but with vanilla
require it means you have to add one require line to
for every handler you want to use. So, I wrote require-fu to provide an alternative.
There are a few prerequisites to this working correctly. Specifically,
- You must pass the full path to the directory you want to include. (Example below.)
- All files under that directory must be requirable files. (e.g. no readme's or other documents)
To use require-fu in the example I presented above, trash
routes/index.js and do the following in
var requireFu = ;app;
The routes folder will be recursively searched for modules, and they will be required with app injected into them. Moreover, you can inject as many things as you'd like.
var requireFu = ;app jobs db;
My name is Matt Farmer. I'm a Software Engineer from Atlanta, GA. As of the time of this writing, By day I'm a code bandit involved at Elemica and Anchor Tab. By night, I'm drawing mad scientist like diagrams on my whiteboard at home, thinking of new ways to take over the Internet and brew the perfect cup of tea.
This was inspired in part by Declan de Wet's Pragmatic Guide to Separating Routes in Express, that got me thinking about applying this concept more generally.