Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Express 4 Dynamically delete middleware from app stack

i'm new in node and i'm trying to add and delete routes "on the fly" in Express 4. I'm adding them using app.use(path,route) and i'm able to locate them in the app._router.stack Array but i'm not able to delete a particular router. Is it possible? I've tried with app._router.stack.splice(myRouterIndex,1) without success.

Here is my example:

for (var i = 0; i < app._router.stack.length;  i++) {
        if(app._router.stack[i].name=='router') {
          console.log(app._router.stack[i].name);     
          app._router.stack.splice[i,1];
          console.log(app._router.stack);          
          break;   
        }
      }

The second console.log(app._router.stack) prints exactly the same as the first one.

like image 452
mapesteguia Avatar asked Dec 01 '25 08:12

mapesteguia


1 Answers

It was a simple mistake due i'm new in Node. Thanks Ben Fortune!

The right syntax is app._router.stack.splice(i,1); due splice is a method. Doing it with brackets [] does not throw any error but simply does not work as expected.

like image 71
mapesteguia Avatar answered Dec 04 '25 00:12

mapesteguia



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!