3

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.

  • `splice` is a method, `splice(i, 1)`. Though since you used it correctly above the example, I'm going to assume a typo? – Ben Fortune Feb 06 '15 at 14:40
  • 1
    possible duplicate of [Remove route mappings in NodeJS Express](http://stackoverflow.com/questions/10378690/remove-route-mappings-in-nodejs-express) – James Feb 06 '15 at 14:43

1 Answers1

4

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.