I wrote middleware for Connect and Express that requires some heavy lifting in its setup method. Due to the nature of the initialization tasks, this material is asynchronous, so I have a problem that the middleware should only be available after starting the initialization.
Currently, I decided to use it with a callback:
function setupMiddleware(callback) { doSomeAsyncInitialization(function () { callback(function (req, res, next) {
It works, but it is not nice for the caller. Instead of doing
app.use(setupMiddleware());
I need to do:
setupMiddleware(functin (middleware) { app.use(middleware); });
Now I was wondering if there is a better approach, for example. Let the middleware initialize in the background and delay all incoming requests until the middleware is ready.
How can i solve this? Any ideas or recommendations I should use here?
Golo roden
source share