1. Use additional middleware which will enable/disable your static route without actually deleting it. This could look like this:
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