Is it possible to "unuse" middleware?

3,864 views
Skip to first unread message

Pete

unread,
Aug 1, 2013, 9:32:37 AM8/1/13
to expre...@googlegroups.com
Specifically I want to mount static content only temporarily and then unmount it.

So to mount it i do something like this:

    var id = uuid.v1();
    app.use('/local/' + id, express.static('./local'));

But later when I know longer need to serve that static content, I would like to do something like:

    app.unuse('/local/' + id);

Or would this work?:

    app.use('/local/' + id);

thanks,
Pete


Justin Russell

unread,
Aug 7, 2013, 8:21:17 AM8/7/13
to expre...@googlegroups.com
You could probably hack together some sort of "unuse" functionality but something like this should work if you're hard up:

app.use('local'/ + id);

app.use(function(req, res, next) {
  if(okToServeLocalStuff(app)) {
    next();
  } else {
    refuseToServeLocalStuff(res);
  }
});

app.use(express.static('./local'));


----------------------------------------------------------------




--
You received this message because you are subscribed to the Google Groups "Express" group.
To unsubscribe from this group and stop receiving emails from it, send an email to express-js+...@googlegroups.com.
To post to this group, send email to expre...@googlegroups.com.
Visit this group at http://groups.google.com/group/express-js.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Kamil Leszczuk

unread,
Aug 6, 2013, 10:02:20 AM8/6/13
to expre...@googlegroups.com
Since there is no reply to your question, I'd note two possibilities:

1. Use additional middleware which will enable/disable your static route without actually deleting it. This could look like this:

// This middleware will enable/disable static route as needed
var StaticToggle = function (path) {
    var _staticEnabled = true;
    var _path = path || "/static";
    
    return {
        middleware: function (req, res, next) {
            console.log(req.originalUrl);
            if (req.originalUrl.indexOf(_path) === 0 && !_staticEnabled) {
                res.send(404);
            } else {
                next();
            }
        },
        toggle: function () {
            console.log('toggle');
            _staticEnabled = !_staticEnabled;
        }
    }
};

var toggle = new StaticToggle('/pub');
// Add toggle middleware before static middleware
app.use(toggle.middleware);
app.use(express.static('static')); 

// Disable static route after 5 sec
setTimeout(toggle.toggle.bind(toggle), 5000);

Obviously, this approach adds some processing overhead for every request. This assumes static content is under '/pub' relative path.

2. Delete route programatically. I believe this is not supported by express (or connect) and could break something:
setTimeout(function deleteStaticRoute() {
    for (var s = 0, l = app.stack.length; s < l; s++) {
        if (app.stack[s].handle && app.stack[s].handle.name === 'staticMiddleware') {
            console.log('removed static route');
            app.stack.splice(s, 1);
            break;
        }

    }
}, 5000);

This assumes that static route handler is called 'staticMiddleware', which is true in connect ~2.8.4).


Hope you find the solution,
Kamil


Patrick Steele-Idem

unread,
Aug 19, 2013, 6:48:31 PM8/19/13
to expre...@googlegroups.com
Hi Pete,

For hot-reloading during development I am using the "express-resetter" module to remove middleware and routes so that new middleware and routes can be registered without restarting the entire application. Even if you don't use the module directly, you can look at the code to see how Express/Connect maintains middleware and routes internally. Here's the module on Github:

I hope that helps.

--Patrick
Reply all
Reply to author
Forward
0 new messages