My app needs to do some action first (and once) and only then start handling the requests and run other actions. I'm looking for some middleware that will get the first action and a bunch of handlers. It should first will invoke the action, and only when it's done it will call all the handlers without calling the first action again.
Edit: suppose my first action takes a while. I can register the app.get handlers in my first action's callback, but then any request arriving before my first action is done will be lost. Instead, I want to store the requests in a queue until the first action finishes, and only then call the handlers on the pending request. Afterwards, I want my middleware to delete itself, so that only the request handlers will handle the new requests.
Do you know such a middleware?
Thanks, Li
Emm... Not sure if I understood you correctly, but...
var done = false;
function myFunc () {
if (done==true)break;
//your code
console.log('First');
//your code
done=true;
}
app.get('/desk/settings', myFunc, function(req, res) {
console.log('Second');
});
Reuest -> Middleware myFunc --> request code.
Not what you're looking for?
You can create your own middleware like this:
app.use(function(req, res, next) {
console.log('first');
if (firstIsReady) {
next();
} else {
res.redirect('/error')
}
})
Make sure you put it before the app.router
. Is that what you are looking for?